我正在尝试从虚拟环境中调用打包为Python的MATLAB包,该包在macOS上运行。
为了在macOS上使用MATLAB运行时,必须更新
然后创建并激活一个Python虚拟环境:
接下来,将针对Python的MATLAB包安装到虚拟环境中:MATLAB包装Python应用程序。
现在尝试在虚拟环境中运行一个导入MATLAB库的脚本:
问题在于据我所知,
为了在macOS上使用MATLAB运行时,必须更新
DYLD_LIBRARY_PATH
以指向MATLAB运行时,以及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}"
然后创建并激活一个Python虚拟环境:
$ python3.6 -m venv py36
$ source py36/bin/activate
接下来,将针对Python的MATLAB包安装到虚拟环境中:MATLAB包装Python应用程序。
(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
现在尝试在虚拟环境中运行一个导入MATLAB库的脚本:
(py36) $ 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.
问题在于据我所知,
mwpython
不能在虚拟环境中使用。有没有一种方法来解决这个问题?我们目前正在努力创建可重复的环境,因为 mwpython
似乎将所有东西硬编码为全局安装。
测试环境如下:
- macOS 10.14.2
- Python 3.6.8 安装自 python.org
- MATLAB Runtime 2018b