Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

import numpy failed after upgrading MacOS to 15.4

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:

  1. Delete and recreate the environment, install the same package backup in yaml file before.
  2. Clean all conda cache, reinstall miniconda, recreate the environment, reinstall the packages.
  3. search and delete the numpy path in the sys.path before import: sys.path = [p for p in sys.path if not os.path.basename(p).lower() == "numpy"]
  4. Clone a new environment, upgrade all packages the latest version.
  5. I have a packed .app file created by the pyinstaller before, which worked well before upgrading the OS, but cannot run now and report the same error.
  6. Another Mac also installed the .app before, but cannot run after upgrading the OS.

Any other way I can solve the problem please..

More details:

  1. previously OS version is 15.3
  2. Macbook Pro M1
  3. upgrade to NumPy 2.2.4 but not solve the problem:numpy 2.0.0-py312hb544834_0 --> 2.2.4-py312h7c1f314_0
  4. no gfortran in conda list
  5. another Macbook Air M1, which works fine with the pyinstaller build .app, and not any other python environment outside the .app, raise the same error after upgrade OS to 15.4
like image 408
WHYF Avatar asked Dec 08 '25 19:12

WHYF


2 Answers

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.

like image 68
Stiin Avatar answered Dec 10 '25 10:12

Stiin


Activate the offending environment then:

conda update --all

worked for me after updating to Mac OS 15.4.1

like image 32
Aaron J Newman Avatar answered Dec 10 '25 09:12

Aaron J Newman