ImportError with libgfortran.5.dylib causing numpy to fail and MNE 1.9.0 not to load

  • MNE version: 1.9.0 (through mac installer)
  • macOS 15.4 on an M3

Hi all - My apologies. I am new to both python and MNE, so I hope this is not a duplicate. I tried searching the key terms I could imagine, and tried a number of actions that I will list below. I also apologize if I fail to describe each step accurately. I am new to python terminology.

MNE 1.9.0 worked perfectly on my mac for several weeks. Then, last week, it threw the error below related to opening libgfortran5.dylib and subsequently loading numpy. This error occurs both when opening PROMPT to activate an environment in the terminal and when using the MNE 1.9.0 interpreter within Spyder and PyCharm (the error occurs when the IDE is first opened).

Since this is about gfortran, I suspect this was caused either by me installing the R package macrtools to make STAN/brms work in R, or by a minor update to macOS from 15.3 to 15.4. Nothing else has substantively changed on my system since the last time MNE worked.

FWIW, installing MNE through the advanced route with Anaconda works. I can point Spyder and PyCharm to that version of the MNE interpreter successfully (inside an Anaconda environment). So this is limited to the prepackaged installer. But I’d like to make the prepackaged installer work again for a number of reasons (working with students, working with the mne-kit-gui, etc).

I will paste the full error from the terminal window below (it is long). I will also list all of the steps I have tried on my own without success.

source /Applications/MNE-Python/1.9.0_0/.mne-python/Menu/mne_open_prompt.sh
(base) jrs9906 ~ % source /Applications/MNE-Python/1.9.0_0/.mne-python/Menu/mne_open_prompt.sh
Using Python 3.12.7 from /Applications/MNE-Python/1.9.0_0/.mne-python/bin/python
Traceback (most recent call last):
  File "/Applications/MNE-Python/1.9.0_0/.mne-python/lib/python3.12/site-packages/numpy/core/__init__.py", line 24, in <module>
    from . import multiarray
  File "/Applications/MNE-Python/1.9.0_0/.mne-python/lib/python3.12/site-packages/numpy/core/multiarray.py", line 10, in <module>
    from . import overrides
  File "/Applications/MNE-Python/1.9.0_0/.mne-python/lib/python3.12/site-packages/numpy/core/overrides.py", line 8, in <module>
    from numpy.core._multiarray_umath import (
ImportError: dlopen(/Applications/MNE-Python/1.9.0_0/.mne-python/lib/python3.12/site-packages/numpy/core/_multiarray_umath.cpython-312-darwin.so, 0x0002): Library not loaded: @rpath/libgfortran.5.dylib
  Referenced from: <C435405B-BFF1-399F-A1EC-BD43418F546A> /Applications/MNE-Python/1.9.0_0/.mne-python/lib/libopenblas.0.dylib
  Reason: tried: '/Applications/MNE-Python/1.9.0_0/.mne-python/lib/libgfortran.5.dylib' (duplicate LC_RPATH '@loader_path'), '/Applications/MNE-Python/1.9.0_0/.mne-python/lib/libgfortran.5.dylib' (duplicate LC_RPATH '@loader_path'), '/Applications/MNE-Python/1.9.0_0/.mne-python/lib/python3.12/site-packages/numpy/core/../../../../libgfortran.5.dylib' (duplicate LC_RPATH '@loader_path'), '/Applications/MNE-Python/1.9.0_0/.mne-python/lib/python3.12/site-packages/numpy/core/../../../../libgfortran.5.dylib' (duplicate LC_RPATH '@loader_path'), '/Applications/MNE-Python/1.9.0_0/.mne-python/bin/../lib/libgfortran.5.dylib' (duplicate LC_RPATH '@loader_path'), '/Applications/MNE-Python/1.9.0_0/.mne-python/bin/../lib/libgfortran.5.dylib' (duplicate LC_RPATH '@loader_path'), '/usr/local/lib/libgfortran.5.dylib' (slice is not valid mach-o file), '/usr/lib/libgfortran.5.dylib' (no such file, not in dyld cache)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Applications/MNE-Python/1.9.0_0/.mne-python/lib/python3.12/site-packages/numpy/__init__.py", line 130, in <module>
    from numpy.__config__ import show as show_config
  File "/Applications/MNE-Python/1.9.0_0/.mne-python/lib/python3.12/site-packages/numpy/__config__.py", line 4, in <module>
    from numpy.core._multiarray_umath import (
  File "/Applications/MNE-Python/1.9.0_0/.mne-python/lib/python3.12/site-packages/numpy/core/__init__.py", line 50, in <module>
    raise ImportError(msg)
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 "/Applications/MNE-Python/1.9.0_0/.mne-python/bin/python3.12"
  * The NumPy version is: "1.26.4"

and make sure that they are the versions you expect.
Please carefully study the documentation linked above for further help.

Original error was: dlopen(/Applications/MNE-Python/1.9.0_0/.mne-python/lib/python3.12/site-packages/numpy/core/_multiarray_umath.cpython-312-darwin.so, 0x0002): Library not loaded: @rpath/libgfortran.5.dylib
  Referenced from: <C435405B-BFF1-399F-A1EC-BD43418F546A> /Applications/MNE-Python/1.9.0_0/.mne-python/lib/libopenblas.0.dylib
  Reason: tried: '/Applications/MNE-Python/1.9.0_0/.mne-python/lib/libgfortran.5.dylib' (duplicate LC_RPATH '@loader_path'), '/Applications/MNE-Python/1.9.0_0/.mne-python/lib/libgfortran.5.dylib' (duplicate LC_RPATH '@loader_path'), '/Applications/MNE-Python/1.9.0_0/.mne-python/lib/python3.12/site-packages/numpy/core/../../../../libgfortran.5.dylib' (duplicate LC_RPATH '@loader_path'), '/Applications/MNE-Python/1.9.0_0/.mne-python/lib/python3.12/site-packages/numpy/core/../../../../libgfortran.5.dylib' (duplicate LC_RPATH '@loader_path'), '/Applications/MNE-Python/1.9.0_0/.mne-python/bin/../lib/libgfortran.5.dylib' (duplicate LC_RPATH '@loader_path'), '/Applications/MNE-Python/1.9.0_0/.mne-python/bin/../lib/libgfortran.5.dylib' (duplicate LC_RPATH '@loader_path'), '/usr/local/lib/libgfortran.5.dylib' (slice is not valid mach-o file), '/usr/lib/libgfortran.5.dylib' (no such file, not in dyld cache)


The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/Applications/MNE-Python/1.9.0_0/.mne-python/bin/mne", line 6, in <module>
    from mne.commands.utils import main
  File "/Applications/MNE-Python/1.9.0_0/.mne-python/lib/python3.12/site-packages/mne/__init__.py", line 35, in <module>
    set_log_level(None, False)
  File "/Applications/MNE-Python/1.9.0_0/.mne-python/lib/python3.12/site-packages/mne/utils/_logging.py", line 210, in set_log_level
    verbose = _parse_verbose(verbose)
              ^^^^^^^^^^^^^^^^^^^^^^^
  File "/Applications/MNE-Python/1.9.0_0/.mne-python/lib/python3.12/site-packages/mne/utils/_logging.py", line 225, in _parse_verbose
    from .check import _check_option, _validate_type
  File "/Applications/MNE-Python/1.9.0_0/.mne-python/lib/python3.12/site-packages/mne/utils/check.py", line 17, in <module>
    import numpy as np
  File "/Applications/MNE-Python/1.9.0_0/.mne-python/lib/python3.12/site-packages/numpy/__init__.py", line 135, in <module>
    raise ImportError(msg) from e
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.
This is 
(mne-1.9.0_0) jrs9906 ~ %

Here is what I have tried to see if I could resolve this:

-uninstall/re-install of MNE
-uninstall/re-install (via command line) of numpy
-upgrading numpy using: pip install numpy --upgrade, then downgrading with pip install ‘numpy<2’ (this looks like it works at first, because PROMPT opens without an error, but running mne kit2fiff leads to the same error message, so it really is about gfortran, not numpy)
-uninstall/re-install CommandLineTools
-copying an alias that points to libgfortran.5.dylib into nearly every location that is mentioned
-uninstall macrtools (and the gfortran that it loaded), uninstall homebrew, then uninstall/re-install MNE
-uninstall/re-install xcode again (for a new instance of gfortran after removing macrtools)

Thank you!
-jon

Update to add: I tried installing MNE python 1.9.0 on a different Mac that had never had MNE installed before and never had any of the R packages that interact with gfortran, but crucially it is upgraded to macOS 15.4. It throws the same error. So I suspect that this is more likely to be about upgrading to macOS 15.4.