首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >用于Python的MATLAB引擎API。错误:用于Python的MATLAB引擎支持Python版本

用于Python的MATLAB引擎API。错误:用于Python的MATLAB引擎支持Python版本
EN

Stack Overflow用户
提问于 2022-07-05 07:31:47
回答 2查看 425关注 0票数 1

我在写硕士论文。我的导演用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。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2022-07-13 15:21:09

最后,我让它起作用了。下面是操作步骤:

如何安装用于Python的MATLAB引擎API。

简短回答:

代码语言:javascript
运行
复制
cd "matlabroot"\extern\engines\python

sudo \path_to_compatible_Python_version setup.py install

长答案:

  1. 您需要正确安装Matlab (我将跳过这一步)
  2. 找到matlabroot (在哪里安装了Matlab )。在Matlab的命令窗口中,键入: 马氏根 他们是你的母校。在我的例子中,是:'/usr/local/MATLAB/R2022a‘
  3. 打开终端并键入: cd "matlabroot/extern/engines/python“ 只需记住将“matlabroot”改为“”(即步骤2中的输出)。
  4. 运行setup.py。2种方式: 4.1你可以试试:(它对我不起作用,但在安装用于Python的MATLAB引擎API中是推荐的 python setup.py安装 4.2如果得到错误: 错误:无法创建“生成”:权限被拒绝 尝试用sudo运行它: sudo python3 setup.py安装 (我把'python‘改为'python3')

那么您可能会得到版本错误。就像:

代码语言:javascript
运行
复制
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. 那,我们做吧。您可以键入终端。 哪条蟒蛇。 这样就可以使用python3所在的位置。(例如,我得到/home/user_name/anaconda3 3/bin/python3 3) 转到目录/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。在我的例子中,如下所示:

代码语言:javascript
运行
复制
sudo /home/user_name/anaconda3/bin/python3.9 setup.py install

所有这些都对我有用。祝好运!

CU

票数 0
EN

Stack Overflow用户

发布于 2022-08-11 13:23:59

要在Ubuntu/Debianforpython3.9 matlab.engine上安装并使用conda环境对其进行配置,请使用以下过程:

  1. 在命令行上激活conda环境 $ conda activate <your_amazing_conda_env>
  2. 然后,安装matlab内核。 $ pip install matlab-kernel
  3. 更改为matlab安装目录 $ cd /usr/local/MATLAB/R2022a/extern/engines/python
  4. 通过直接指向您的anaconda环境所在的位置来安装matlab.engine。例如: $ sudo "/home/mario/anaconda3/envs/chamois/bin/python" setup.py install

注意,在最后一步中您需要管理特权。

如果安装成功,您将得到堆栈跟踪,如下所示:

代码语言:javascript
运行
复制
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来测试安装的工作。

代码语言:javascript
运行
复制
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']
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72865572

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档