How to call MATLAB library from Python virtual environment on Mac

조회 수: 18 (최근 30일)
J Hagen
J Hagen 2019년 1월 18일
편집: J Hagen 2019년 1월 18일
I am trying to call a MATLAB package packaged for Python on macOS from a virtual environment.
In order to use the MATLAB runtime on macOS, the DYLD_LIBRARY_PATH must be updated to point to the MATLAB Runtime as well as libpython3.6.dylib.
export DYLD_LIBRARY_PATH="/Applications/MATLAB/MATLAB_Runtime/v95/runtime/maci64:/Applications/MATLAB/MATLAB_Runtime/v95/sys/os/maci64:/Applications/MATLAB/MATLAB_Runtime/v95/bin/maci64:/Library/Frameworks/Python.framework/Versions/3.6/lib:${DYLD_LIBRARY_PATH}"
Then create and activate a Python virtual environment:
$ python3.6 -m venv py36
$ source py36/bin/activate
Next install the MATLAB packaged for Python application into the virtual environment:
(py36) $ cd /Applications/my_matlab_app/application
(py36) $ python setup.py install
(py36) $ pip list
Package Version
---------------------- -------
matlabruntimeforpython R2018b
pip
18.1
setuptools
40.6.2
Now attempt to run a script that imports your MATLAB library within the virtual environment:
$ python matlab_test.py
Exception caught during initialization of Python interface. Details: On the Mac, use 'mwpython' rather than 'python' to start a script or session that will call deployed MATLAB code from Python.
Traceback (most recent call last):
File "matlab_test.py", line 26, in <module>
import my_matlab_app
File "/Users/user/venv/py36/lib/python3.6/site-packages/my_matlab_app/__init__.py", line 283, in <module>
_pir
.import_cppext()
File "/Users/user/venv/py36/lib/python3.6/site-packages/my_matlab_app/__init__.py", line 276, in import_cppext
self.cppext_handle = importlib.import_module("matlabruntimeforpython" + self.interpreter_version)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/importlib/__init__.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
RuntimeError: On the Mac, use 'mwpython' rather than 'python' to start a script or session that will call deployed MATLAB code from Python.
The problem is that as far as I can tell, mwpython can't be used within virtual environments. Is there a way to work around this? We're currently struggling making repeatable environments because mwpython seems to hard code everything to be globally installed.
Tested on:
  • macOS 10.14.2
  • Python 3.6.8 installed from python.org
  • MATLAB Runtime 2018b

답변 (0개)

카테고리

Help CenterFile Exchange에서 Call Python from MATLAB에 대해 자세히 알아보기

태그

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by