Skip to content

Conversation

@johmedr
Copy link

@johmedr johmedr commented Oct 6, 2025

Following discussions with Robert at the SPM Homecoming, I have generated a first Python wrap of FieldTrip version 20241219. As the compiled package generated by Matlab is too large to be updated on Github, it is distributed as an artefact for each release -- and should be updated and distributed through PyPI later on. For now, it can be found in the latest release in my fork. It can be installed directly using pip:

pip install https://github.com/johmedr/fieldtrip-python/releases/latest/download/fieldtrip_python-20241219b1-py3-none-any.whl

I have only tried basic functionality so far.

Please go ahead with trying the package and let me know what works and what does not!

@robertoostenveld
Copy link
Member

Thanks Johan, I will try it out!

For reference, this builds on https://github.com/spm/spm-python. Since I have MATLAB 2025b on my laptop, I will initially work with Python 3.13 in a Conda evvironment.

@robertoostenveld
Copy link
Member

I started by creating a conda environment

conda create -n fieldtrip python=3.13
conda activate fieldtrip

Then pip installed from your GH branch, resulting in

Successfully installed fieldtrip-python-20241219b1 matlab-runtime-0.0.6 mpython-core-25.4rc1 numpy-2.3.4

Following https://github.com/spm/spm-python/blob/main/README.md, I tried option 1 (start fieldtrip, or spm), that did not work (note: not urgent right now, just writing it down for completeness). Option 2 is basically what I did. I then did

mac084> install_matlab_runtime --version R2025b --yes
Do not install: /Applications/MATLAB/MATLAB_Runtime/R2025b

mac084> install_matlab_runtime --version R2025b
Runtime already exists in a different location (/Applications/MATLAB_R2025b.app)

So it apparently already finds the runtime which is part of my MATLAB installation and (as expected) does not need to install the runtime environment again.

I then started python and from fieldtrip import * and get this warning, which I don't think is too serious at the moment

UserWarning: Since scipy.sparse is not available, sparse matrices will be implemented as dense matrices, which can lead to unsubstainable memory usage. If this is an issue, install scipy in your python environment 

Then I do

>>> cfg = Struct()
>>> cfg.dataset = 'Subject01.ds'
>>> data = ft_preprocessing(cfg)
Initializing Matlab Runtime...
Traceback (most recent call last):
  File "<python-input-6>", line 1, in <module>
    data = ft_preprocessing(cfg)
  File "/Users/roboos/miniconda3/envs/fieldtrip/lib/python3.13/site-packages/fieldtrip/ft_preprocessing.py", line 160, in ft_preprocessing
    return Runtime.call("ft_preprocessing", *args, **kwargs)
           ~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/roboos/miniconda3/envs/fieldtrip/lib/python3.13/site-packages/mpython/runtime.py", line 45, in call
    res = cls.instance().mpython_endpoint(fn, *args, **kwargs)
  File "/Users/roboos/miniconda3/envs/fieldtrip/lib/python3.13/site-packages/fieldtrip/_fieldtrip/_endpoint.py", line 23, in __call__
    self._init_endpoint()
    ~~~~~~~~~~~~~~~~~~~^^
  File "/Users/roboos/miniconda3/envs/fieldtrip/lib/python3.13/site-packages/fieldtrip/_fieldtrip/_endpoint.py", line 16, in _init_endpoint
    matlab_runtime.init(__matlab_release__, install_if_missing=True)
    ~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/roboos/miniconda3/envs/fieldtrip/lib/python3.13/site-packages/matlab_runtime/impl.py", line 344, in init
    importlib.import_module(_CPP)
    ~~~~~~~~~~~~~~~~~~~~~~~^^^^^^
  File "/Users/roboos/miniconda3/envs/fieldtrip/lib/python3.13/importlib/__init__.py", line 88, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
           ~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "<frozen importlib._bootstrap>", line 1387, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1360, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1324, in _find_and_load_unlocked
ModuleNotFoundError: No module named 'matlabruntimeforpython_abi3'

I found and followed instructions here, but that did not resolve it.

@robertoostenveld
Copy link
Member

I tried around various things and at a certain point was warned that the matlab engine in python does not work with 3.13, so installed python 3.12 instead. That did not resolve it.

@robertoostenveld
Copy link
Member

I made a step forward. I installed MATLAB runtime R2025b separately from https://nl.mathworks.com/products/compiler/matlab-runtime.html and added

export DYLD_LIBRARY_PATH="/Applications/MATLAB/MATLAB_Runtime/R2025b/runtime/maca64:$DYLD_LIBRARY_PATH"
export DYLD_LIBRARY_PATH="/Applications/MATLAB/MATLAB_Runtime/R2025b/bin/maca64:$DYLD_LIBRARY_PATH"
export DYLD_LIBRARY_PATH="/Applications/MATLAB/MATLAB_Runtime/R2025b/sys/os/maca64:$DYLD_LIBRARY_PATH"
export DYLD_LIBRARY_PATH="/Applications/MATLAB/MATLAB_Runtime/R2025b/extern/bin/maca64:$DYLD_LIBRARY_PATH"

to my DYLD_LIBRARY_PATH. When executing fieldtrip.ft_version(), it now takes some time (it appears something is happening in the background) and then I get

RuntimeError: On the Mac, use 'mwpython' rather than 'python' to start a script or session that will call deployed MATLAB code from Python.

So I am further than I was before. However,

mac084> mwpython
-bash: mwpython: command not found

@robertoostenveld
Copy link
Member

Ah, found it in /Applications/MATLAB/MATLAB_Runtime/R2025b/bin/mwpython. Using that and

>>> import fieldtrip
>>> fieldtrip.ft_version()
Initializing Matlab Runtime...

it does not print anything else and stays at this forever without obvious progress.

@johmedr
Copy link
Author

johmedr commented Oct 27, 2025

Hi @robertoostenveld, thank you for looking into this. Regarding the runtime, I would recommend installing @balbasty/matlab-runtime through pip install matlab-runtime. This will give you access to mwpython2, an executable that works better on Mac than Matlab's mwpython.

@robertoostenveld
Copy link
Member

I installed matlab-runtime and tried with that, but same symptoms: it does not give an erron, but basically sits there (for <15 minutes) without anything happening and with 100% CPU load in the ActivityMonitor.

When I stop it with Ctrl-C, I get this

Initializing Matlab Runtime...
^CTraceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/roboos/miniconda3/envs/fieldtrip/lib/python3.12/site-packages/fieldtrip/__utilities/ft_version.py", line 69, in ft_version
    return Runtime.call("ft_version", *args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/roboos/miniconda3/envs/fieldtrip/lib/python3.12/site-packages/mpython/runtime.py", line 45, in call
    res = cls.instance().mpython_endpoint(fn, *args, **kwargs)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/roboos/miniconda3/envs/fieldtrip/lib/python3.12/site-packages/fieldtrip/_fieldtrip/_endpoint.py", line 23, in __call__
    self._init_endpoint()
  File "/Users/roboos/miniconda3/envs/fieldtrip/lib/python3.12/site-packages/fieldtrip/_fieldtrip/_endpoint.py", line 16, in _init_endpoint
    matlab_runtime.init(__matlab_release__, install_if_missing=True)
  File "/Users/roboos/miniconda3/envs/fieldtrip/lib/python3.12/site-packages/matlab_runtime/impl.py", line 349, in init
    current_version = guess_pymatlab_version(matlab)
                      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/roboos/miniconda3/envs/fieldtrip/lib/python3.12/site-packages/matlab_runtime/utils.py", line 485, in guess_pymatlab_version
    return _guess_pymatlab_version(matlab, "version")
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/roboos/miniconda3/envs/fieldtrip/lib/python3.12/site-packages/matlab_runtime/utils.py", line 494, in _guess_pymatlab_version
    return _guess_matlab_version(matlab.get_arch_filename(), key)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/roboos/miniconda3/envs/fieldtrip/lib/python3.12/site-packages/matlab_runtime/utils.py", line 515, in _guess_matlab_version
    path = op.dirname(path)
           ^^^^^^^^^^^^^^^^
  File "<frozen posixpath>", line 179, in dirname
KeyboardInterrupt

>>> Traceback (most recent call last):
  File "/Users/roboos/miniconda3/envs/fieldtrip/bin/mwpython2", line 7, in <module>
    sys.exit(mwpython2())
             ^^^^^^^^^^^
  File "/Users/roboos/miniconda3/envs/fieldtrip/lib/python3.12/site-packages/matlab_runtime/cli.py", line 275, in mpython
    p = subprocess.run([mwpython_app, *args_and_version], **opt)
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/roboos/miniconda3/envs/fieldtrip/lib/python3.12/subprocess.py", line 550, in run
    stdout, stderr = process.communicate(input, timeout=timeout)
                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/roboos/miniconda3/envs/fieldtrip/lib/python3.12/subprocess.py", line 1201, in communicate
    self.wait()
  File "/Users/roboos/miniconda3/envs/fieldtrip/lib/python3.12/subprocess.py", line 1264, in wait
    return self._wait(timeout=timeout)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/roboos/miniconda3/envs/fieldtrip/lib/python3.12/subprocess.py", line 2053, in _wait
    (pid, sts) = self._try_wait(0)
                 ^^^^^^^^^^^^^^^^^
  File "/Users/roboos/miniconda3/envs/fieldtrip/lib/python3.12/subprocess.py", line 2011, in _try_wait
    (pid, sts) = os.waitpid(self.pid, wait_flags)
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
KeyboardInterrupt

I don't know whether this means anything to you.

@robertoostenveld
Copy link
Member

I wonder whether it might have something to do with macOS security settings. I am reminded of this because of this issue that we also encountered on a user's computer this week. The mwpython2 loads a lot of DLLs (or the macOS equivalent).

I have in my System Settings this

Screenshot 2025-10-30 at 15 27 49

and I am not able to select "from Anywhere". It makes me think that perhaps it is trying to pop up a window about a permission error or limitation, but that the window does not show.

@johmedr
Copy link
Author

johmedr commented Oct 30, 2025

Thank you for trying this. It seems that matlab-runtime stalls trying to locate the metadata containing the runtime version. It looks for VersionInfo.xml somewhere in the path returned by the internal matlab package, but for some reason it cannot locate this file. It could be that the wrong matlab package is used to get the path to the VersionInfo. Could you check that Matlab Compiler and Matlab Compiler SDK are installed?

Just for reference, the Matlab Runtime is installed either independently from Matlab by downloading the installer, in which case it gets installed in /Applications/MATLAB/MATLAB_Runtime/R2025b on Mac, or shipped with the Matlab Compiler SDK in Matlab, in which case it lives in /Applications/MATLAB_R2025b.app.

Also inviting @balbasty to the discussion, in case you already came across Robert's issue before.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants