Import Python user modules in Matlab

Matlab can call user Python modules. The Python program must work first with Python alone for it to work in Matlab. For concurrent Python modules using asyncio, you may need to create a shim function to allow Matlab to call the Python module. Anaconda Python works fine from Matlab.

Configure Matlab for Python: Matlab is designed to work with specific Python versions for each Matlab version. Matlab will not specifically tell you when you’re using an incompatible Python version, but you may get unstable operation or errors. pyenv Python environment manager configures the Python interface.

The Python executable is found from Terminal / Command Prompt:

python -c "import sys; print(sys.executable)"

For Windows, it may be like C:/Miniconda3/python.exe and for macOS / Linux it may be like ~/Miniconda3/python.

This Matlab command is persistent–Matlab remembers this Python choice even after restarting Matlab.

pyenv('Version', 'C:/Miniconda3/python.exe')

Verify Matlab → Python config by typing pyenv from within Matlab.

Troubleshooting

If only Python standard library modules work, and even commonly used modules like Numpy will not work, errors may occur from Matlab like:

py.numpy.arange(1)
Unable to resolve the name py.numpy.arange

Try diagnosing the issue from Matlab like:

pyenv

help('py.numpy')

Verify the desired Python install is being used, and that there isn’t an error as below. If such an error occurs, check the PATH as seen by Matlab by:

getenv('PATH')

If the Python directories are missing, this may be why user modules are not importable in Python. These paths can be added to Matlab. On Windows, the necessary path is like ~/Miniconda3/Library/bin, the directory with lots of *.{so,dylib,dll} files and the fix is like:

setenv('PATH', fullfile(getenv("USERPROFILE"), "Miniconda3/Library/bin") + pathsep, getenv('PATH'))

The install path on non-Windows OS is like ~/miniconda3/lib.

This command can be issued after a Python command was attempted, it’s not necessary to restart Matlab. Note that this needs to be in PATH, since PYTHONPATH does not help in this case. This change does not persist across Matlab sessions. If it works for you, put it in the ~/Documents/MATLAB/startup.m file to have it reload each time Matlab is started.

Here’s the error that’s fixed by the procedure above:

problem in numpy - ImportError:

IMPORTANT: PLEASE READ THIS FOR ADVICE ON HOW TO SOLVE THIS ISSUE!

Importing the numpy c-extensions failed.
- Try uninstalling and reinstalling numpy.
- If you have already done that, then:
  1. Check that you expected to use Python3.7 from "C:/Miniconda3/python.exe",
     and that you have no directories in your PATH or PYTHONPATH that can
     interfere with the Python and numpy version "1.17.4" you're trying to use.
  2. If (1) looks fine, you can open a new issue at
     https://github.com/numpy/numpy/issues.  Please include details on:
     - how you installed Python
     - how you installed numpy
     - your operating system
     - whether or not you have multiple versions of Python installed
     - if you built from source, your compiler versions and ideally a build log

- If you're working with a numpy git repository, try `git clean -xdf`
  (removes all files not under version control) and rebuild numpy.

Note: this error has many possible causes, so please don't comment on
an existing issue about this - open a new one instead.

Original error was: DLL load failed: The specified module could not be found.
  • Python module import is implicit in the Matlab → Python module function call. There is no need to import numpy etc. from Matlab
  • Python executable choice persists across Matlab sessions–Matlab “remembers” even after you restart Matlab or reboot the computer.
  • editing imported Python module code requires restarting Matlab to take effect.