After upgrading MacOS to 15.4. the import failed: Exception has occurred: ImportError Error importing numpy: you should not try to import numpy from its source directory; please exit the numpy source tree, and relaunch your python interpreter from there.
Error importing numpy: you should not try to import numpy from
its source directory; please exit the numpy source tree, and relaunch
your python interpreter from there.
ImportError: dlopen(/opt/miniconda3/envs/ho/lib/python3.12/site-packages/numpy/_core/_multiarray_umath.cpython-312-darwin.so, 0x0002): Library not loaded: @rpath/libgfortran.5.dylib
Referenced from: <0B9C315B-A1DD-3527-88DB-4B90531D343F> /opt/miniconda3/envs/ho/lib/libopenblas.0.dylib
Reason: tried: '/opt/miniconda3/envs/ho/lib/libgfortran.5.dylib' (duplicate LC_RPATH '@loader_path'), '/opt/miniconda3/envs/ho/lib/libgfortran.5.dylib' (duplicate LC_RPATH '@loader_path'), '/opt/miniconda3/envs/ho/lib/python3.12/site-packages/numpy/_core/../../../../libgfortran.5.dylib' (duplicate LC_RPATH '@loader_path'), '/opt/miniconda3/envs/ho/lib/python3.12/site-packages/numpy/_core/../../../../libgfortran.5.dylib' (duplicate LC_RPATH '@loader_path'), '/opt/miniconda3/envs/ho/bin/../lib/libgfortran.5.dylib' (duplicate LC_RPATH '@loader_path'), '/opt/miniconda3/envs/ho/bin/../lib/libgfortran.5.dylib' (duplicate LC_RPATH '@loader_path'), '/usr/local/lib/libgfortran.5.dylib' (no such file), '/usr/lib/libgfortran.5.dylib' (no such file, not in dyld cache)
During handling of the above exception, another exception occurred:
ImportError:
IMPORTANT: PLEASE READ THIS FOR ADVICE ON HOW TO SOLVE THIS ISSUE!
Importing the numpy C-extensions failed. This error can happen for
many reasons, often due to issues with your setup or how NumPy was
installed.
We have compiled some common reasons and troubleshooting tips at:
https://numpy.org/devdocs/user/troubleshooting-importerror.html
Please note and check the following:
* The Python version is: Python3.12 from "/opt/miniconda3/envs/ho/bin/python"
* The NumPy version is: "2.0.0"
and make sure that they are the versions you expect.
Please carefully study the documentation linked above for further help.
Original error was: dlopen(/opt/miniconda3/envs/ho/lib/python3.12/site-packages/numpy/_core/_multiarray_umath.cpython-312-darwin.so, 0x0002): Library not loaded: @rpath/libgfortran.5.dylib
Referenced from: <0B9C315B-A1DD-3527-88DB-4B90531D343F> /opt/miniconda3/envs/ho/lib/libopenblas.0.dylib
Reason: tried: '/opt/miniconda3/envs/ho/lib/libgfortran.5.dylib' (duplicate LC_RPATH '@loader_path'), '/opt/miniconda3/envs/ho/lib/libgfortran.5.dylib' (duplicate LC_RPATH '@loader_path'), '/opt/miniconda3/envs/ho/lib/python3.12/site-packages/numpy/_core/../../../../libgfortran.5.dylib' (duplicate LC_RPATH '@loader_path'), '/opt/miniconda3/envs/ho/lib/python3.12/site-packages/numpy/_core/../../../../libgfortran.5.dylib' (duplicate LC_RPATH '@loader_path'), '/opt/miniconda3/envs/ho/bin/../lib/libgfortran.5.dylib' (duplicate LC_RPATH '@loader_path'), '/opt/miniconda3/envs/ho/bin/../lib/libgfortran.5.dylib' (duplicate LC_RPATH '@loader_path'), '/usr/local/lib/libgfortran.5.dylib' (no such file), '/usr/lib/libgfortran.5.dylib' (no such file, not in dyld cache)
The above exception was the direct cause of the following exception:
File "/Users/yf/Library/CloudStorage/OneDrive-Personal/Documents/Projects/hohoho/playground.py", line 1, in <module>
import numpy as np
ImportError: Error importing numpy: you should not try to import numpy from
its source directory; please exit the numpy source tree, and relaunch
your python interpreter from there.
Everything was OK yesterday. The environment is created by miniconda. Python is 3.12.4, numpy is 2.0.0
I tried all ways below, but cannot solve the exception:
Any other way I can solve the problem please..
More details:
You can run the following from within the offending environment.
'Never run commands from the internet without checking out the script!' - source
The command:
curl -fsSL 'https://gist.githubusercontent.com/basnijholt/811307f125619c53ef876f8ea6ab6685/raw/c678b893c223c3ec3dec3bdb67937c5adc2fab7f/fix.sh' | bash
The script:
#!/bin/bash
# filepath: fix_lib_paths.sh
# https://github.com/conda-forge/numpy-feedstock/issues/347#issuecomment-2746317575
# Activate the conda environment and run this script.
set -e
LIB_PATH="$CONDA_PREFIX/lib"
# Find all dylib files
find "$LIB_PATH" -name "*.dylib" -o -name "*.so" | while read -r library; do
echo "Processing $library..."
# Extract all LC_RPATH entries
rpaths=$(otool -l "$library" | grep -A2 LC_RPATH | grep "path " | awk '{print $2}')
# Create a temporary file to track seen rpaths
temp_file=$(mktemp)
# Check for duplicates and remove them
echo "$rpaths" | while read -r rpath; do
if [[ -z "$rpath" ]]; then
continue
fi
if grep -q "^$rpath$" "$temp_file"; then
echo " Removing duplicate RPATH: $rpath"
install_name_tool -delete_rpath "$rpath" "$library" || true
else
echo "$rpath" >> "$temp_file"
echo " Keeping RPATH: $rpath"
fi
done
# Re-sign the library
echo " Re-signing $library"
codesign --force --sign - "$library" || echo " Warning: Could not sign $library"
# Clean up the temporary file
rm -f "$temp_file"
echo "Done with $library"
echo "-----------------------"
done
echo "All libraries processed!"
This worked for conda and mamba and should also work for pixi. It took less than a minute for a small environment.
Activate the offending environment then:
conda update --all
worked for me after updating to Mac OS 15.4.1
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With