适用于Python的MatLab引擎API,错误:适用于Python的MatLab引擎支持Python版本

wvt8vs2t  于 2022-11-15  发布在  Matlab
关注(0)|答案(2)|浏览(146)

我正在写我的硕士论文。我的导演用的是MatLab,我用的是Python。所以我需要在Python上运行他的MatLab脚本。关于这个主题有很多问题。
我试图安装用于Python的MATLAB引擎API(https://es.mathworks.com/help/matlab/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。

o2g1uqev

o2g1uqev1#

最后,我让它工作起来。以下是方法:
如何安装适用于Python的matlab引擎API。

简单回答:

cd "matlabroot"\extern\engines\python

sudo \path_to_compatible_Python_version setup.py install

长长的答案:

1.您需要正确安装MatLab(我将跳过这一步)
1.找到matlab根目录(其中安装了您的MatLab)。在Matlab的命令窗口中,键入:

matlabroot

ANS就是“你的草根”。在我的例子中,是:‘/usr/local/matlab/R2022a’
1.打开终端,输入:

cd "matlabroot/extern/engines/python"

只需记住将‘matLabroot’更改为‘Your matLabroot’(即步骤2中的输出)。
1.运行setup.py。2种方式:
4.1可以试试:(不适合我,但在Install MATLAB Engine API for Python中推荐

python setup.py install

4.2如果您收到以下错误:

error: could not create 'build': Permission denied

尝试使用sudo运行它:

sudo python3 setup.py install

(我把‘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
1.让我们开始吧。你可以在终端上打字

which python.

这给了python3所在的位置以帮助。(例如,我得到/home/用户名/anaconda3/bin/python3)
转到目录/home/user_name/anaconda3/bin(我们省略了python3,因为它指的是符号链接,而不是目录)
在那里我们应该看到(键入ls)一个名为python3的符号链接(青色中的单词
我们可以通过以下方式find the original file of a symbolic link

readlink -f python3

机会很高,它在同一个目录中(如果不是,就去那里)。我们需要搜索与您的MatLab版本兼容的Python版本。您可以通过OSErrorVersions of Python Compatible with MATLAB Products by Releas知道这一点
在同一目录中再次输入ls,然后搜索与您的MatLab(绿色单词)兼容的Python。在我的例子中,我找到了python3.9,它可以在我的MatLab版本中使用。
现在,您已经找到了兼容版本的Python,重复步骤3。运行setup.py,但这次使用我们刚刚找到的新的Python方向更改python3。在我的例子中,如下所示:

sudo /home/user_name/anaconda3/bin/python3.9 setup.py install

所有这些对我来说都奏效了。祝好运!

mwngjboj

mwngjboj2#

要在Ubuntu/Debian上安装matlab.engine for python3.9将其配置为conda环境,请使用以下步骤:
1.在命令行上激活您的Conda环境
$ conda activate <your_amazing_conda_env>
1.然后,安装MatLab内核
$ pip install matlab-kernel
1.切换到matlab安装目录
$ cd /usr/local/MATLAB/R2022a/extern/engines/python
1.通过直接指向您的Python环境所在的位置来安装matLab.Engine。例如
$ 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']

相关问题