我在写硕士论文。我的导演用MATLAB,我用Python。所以我需要在Python上运行他的MATLAB脚本。关于这个话题有很多问题。
我试着安装(external/install-the-matlab-engine-for-python.html)。我遇到了以下问题:
Error: MATLAB Engine for Python supports Python version 2.7, 3.7, 3.8, and 3.9, but your version of Python is 3.10
我有哪些选择?
我还有一个疑问:我检查了Python版本
python3 --version
我得到一个输出:Python3.9.7。那我就不明白这个错误了。
我用的是Ubuntu和Anaconda。
发布于 2022-07-13 15:21:09
最后,我让它起作用了。下面是操作步骤:
如何安装用于Python的MATLAB引擎API。
简短回答:
cd "matlabroot"\extern\engines\python
sudo \path_to_compatible_Python_version setup.py install
长答案:
setup.py
。2种方式:
4.1你可以试试:(它对我不起作用,但在安装用于Python的MATLAB引擎API中是推荐的
python setup.py安装
4.2如果得到错误:
错误:无法创建“生成”:权限被拒绝
尝试用sudo
运行它:
sudo python3 setup.py安装
(我把'python‘改为'python3')那么您可能会得到版本错误。就像:
EnvironmentError('MATLAB Engine for Python supports Python version')
OSError: MATLAB Engine for Python supports Python version [···], but your version of Python is [···].
您可以使用python -V
检查您的Python。
您有可能安装了不止一个版本的Python。您必须确保使用到所需python.exe
的直接路径,而不是使用快捷方式python3
。
/home/user_name/anaconda3/bin
(我们省略了python3,因为它引用的是一个符号链接,而不是目录)
在这里我们应该看到(ls
类型)一个名为python3
的符号链接(在Cyan中的单词)
我们可以通过以下方式进行查找符号链接的原始文件:
读链接-f python3
机会很大,它在同一个目录中(如果没有,就去那里)。我们需要搜索与matlab版本兼容的python版本。您可以通过OSError
或与Releas的MATLAB产品兼容的Python版本知道这一点。
再次在同一个目录中键入ls
,并搜索与Matlab兼容的python (绿色单词)。在我的例子中,我找到了python3.9
,它与我的Matlab版本一起工作。现在,您已经找到了一个兼容的Python版本,重复步骤3。运行setup.py
,但是这次用我们刚刚找到的新的python方向来更改python3
。在我的例子中,如下所示:
sudo /home/user_name/anaconda3/bin/python3.9 setup.py install
所有这些都对我有用。祝好运!
CU
发布于 2022-08-11 13:23:59
要在Ubuntu/Debianforpython3.9 matlab.engine
上安装并使用conda环境对其进行配置,请使用以下过程:
$ conda activate <your_amazing_conda_env>
$ pip install matlab-kernel
$ cd /usr/local/MATLAB/R2022a/extern/engines/python
$ sudo "/home/mario/anaconda3/envs/chamois/bin/python" setup.py install
注意,在最后一步中您需要管理特权。
如果安装成功,您将得到堆栈跟踪,如下所示:
reading manifest file 'dist/matlabengineforpython.egg-info/SOURCES.txt'
writing manifest file 'dist/matlabengineforpython.egg-info/SOURCES.txt'
installing library code to build/bdist.linux-x86_64/egg
running install_lib
running build_py
creating build/bdist.linux-x86_64/egg
creating build/bdist.linux-x86_64/egg/matlab
copying build/lib/matlab/__init__.py -> build/bdist.linux-x86_64/egg/matlab
creating build/bdist.linux-x86_64/egg/matlab/engine
copying build/lib/matlab/engine/engineerror.py -> build/bdist.linux-x86_64/egg/matlab/engine
copying build/lib/matlab/engine/basefuture.py -> build/bdist.linux-x86_64/egg/matlab/engine
copying build/lib/matlab/engine/futureresult.py -> build/bdist.linux-x86_64/egg/matlab/engine
copying build/lib/matlab/engine/__init__.py -> build/bdist.linux-x86_64/egg/matlab/engine
copying build/lib/matlab/engine/_arch.txt -> build/bdist.linux-x86_64/egg/matlab/engine
copying build/lib/matlab/engine/enginehelper.py -> build/bdist.linux-x86_64/egg/matlab/engine
copying build/lib/matlab/engine/matlabfuture.py -> build/bdist.linux-x86_64/egg/matlab/engine
copying build/lib/matlab/engine/enginesession.py -> build/bdist.linux-x86_64/egg/matlab/engine
copying build/lib/matlab/engine/fevalfuture.py -> build/bdist.linux-x86_64/egg/matlab/engine
copying build/lib/matlab/engine/matlabengine.py -> build/bdist.linux-x86_64/egg/matlab/engine
byte-compiling build/bdist.linux-x86_64/egg/matlab/__init__.py to __init__.cpython-39.pyc
byte-compiling build/bdist.linux-x86_64/egg/matlab/engine/engineerror.py to engineerror.cpython-39.pyc
byte-compiling build/bdist.linux-x86_64/egg/matlab/engine/basefuture.py to basefuture.cpython-39.pyc
byte-compiling build/bdist.linux-x86_64/egg/matlab/engine/futureresult.py to futureresult.cpython-39.pyc
byte-compiling build/bdist.linux-x86_64/egg/matlab/engine/__init__.py to __init__.cpython-39.pyc
byte-compiling build/bdist.linux-x86_64/egg/matlab/engine/enginehelper.py to enginehelper.cpython-39.pyc
byte-compiling build/bdist.linux-x86_64/egg/matlab/engine/matlabfuture.py to matlabfuture.cpython-39.pyc
byte-compiling build/bdist.linux-x86_64/egg/matlab/engine/enginesession.py to enginesession.cpython-39.pyc
byte-compiling build/bdist.linux-x86_64/egg/matlab/engine/fevalfuture.py to fevalfuture.cpython-39.pyc
byte-compiling build/bdist.linux-x86_64/egg/matlab/engine/matlabengine.py to matlabengine.cpython-39.pyc
creating build/bdist.linux-x86_64/egg/EGG-INFO
copying dist/matlabengineforpython.egg-info/PKG-INFO -> build/bdist.linux-x86_64/egg/EGG-INFO
copying dist/matlabengineforpython.egg-info/SOURCES.txt -> build/bdist.linux-x86_64/egg/EGG-INFO
copying dist/matlabengineforpython.egg-info/dependency_links.txt -> build/bdist.linux-x86_64/egg/EGG-INFO
copying dist/matlabengineforpython.egg-info/top_level.txt -> build/bdist.linux-x86_64/egg/EGG-INFO
zip_safe flag not set; analyzing archive contents...
matlab.__pycache__.__init__.cpython-39: module references __file__
matlab.__pycache__.__init__.cpython-39: module references __path__
matlab.engine.__pycache__.__init__.cpython-39: module references __file__
creating 'dist/matlabengineforpython-R2022a-py3.9.egg' and adding 'build/bdist.linux-x86_64/egg' to it
removing 'build/bdist.linux-x86_64/egg' (and everything under it)
Processing matlabengineforpython-R2022a-py3.9.egg
removing '/home/mario/anaconda3/envs/chamois/lib/python3.9/site-packages/matlabengineforpython-R2022a-py3.9.egg' (and everything under it)
creating /home/mario/anaconda3/envs/chamois/lib/python3.9/site-packages/matlabengineforpython-R2022a-py3.9.egg
Extracting matlabengineforpython-R2022a-py3.9.egg to /home/mario/anaconda3/envs/chamois/lib/python3.9/site-packages
matlabengineforpython R2022a is already the active version in easy-install.pth
Installed /home/mario/anaconda3/envs/chamois/lib/python3.9/site-packages/matlabengineforpython-R2022a-py3.9.egg
Processing dependencies for matlabengineforpython===R2022a
Finished processing dependencies for matlabengineforpython===R2022a
之后,您可以通过在python终端中导入matlab.engine来测试安装的工作。
In [1]: import matlab.engine
In [2]: matlab.engine.__spec__
Out[2]: ModuleSpec(name='matlab.engine', loader=<_frozen_importlib_external.SourceFileLoader object at 0x7fe58c4514f0>, origin='/home/mario/anaconda3/envs/chamois/lib/python3.9/site-packages/matlabengineforpython-R2022a-py3.9.egg/matlab/engine/__init__.py', submodule_search_locations=['/home/mario/anaconda3/envs/chamois/lib/python3.9/site-packages/matlabengineforpython-R2022a-py3.9.egg/matlab/engine'])
In [3]: matlab.engine.__name__
Out[3]: 'matlab.engine'
In [4]: matlab.engine.__path__
Out[4]: ['/home/mario/anaconda3/envs/chamois/lib/python3.9/site-packages/matlabengineforpython-R2022a-py3.9.egg/matlab/engine']
https://stackoverflow.com/questions/72865572
复制相似问题