简体   繁体   English

使用 Python 运行 Matlab 会出现“No module named matlab.engine”错误

[英]Running Matlab using Python gives 'No module named matlab.engine' error

I am trying to run Matlab code using Python.我正在尝试使用 Python 运行 Matlab 代码。 I tried to follow the instructions given on this Mathworks page .我试图按照此Mathworks 页面上给出的说明进行操作。

When trying to import Matlab though Python, it was installed using pip install matlab .当尝试通过 Python 导入 Matlab 时,它是使用pip install matlab的。

However, importing matlab.engine gives the error No module named 'matlab.engine'; 'matlab' is not a package但是,导入matlab.engine会出现错误No module named 'matlab.engine'; 'matlab' is not a package No module named 'matlab.engine'; 'matlab' is not a package . No module named 'matlab.engine'; 'matlab' is not a package It cannot be installed using pip install engine either.它也不能使用pip install engine

How can I get the code running?我怎样才能让代码运行? The Python code I'm running is as below:我正在运行的 Python 代码如下:

import matlab.engine
eng = matlab.engine.start_matlab()
eng.triarea(nargout=0)

Python version - 3.5 Python 版本 - 3.5

Matlab version - 8.5.0.197613 (R2015a) Matlab 版本 - 8.5.0.197613 (R2015a)

You need to install the Matlab Engine for Python, and it cannot be installed using pip.需要安装 Matlab Engine for Python,不能使用 pip 安装。 Try the instructions listed here .请尝试此处列出的说明。 I have listed the instructions briefly below:我在下面简要列出了说明:

  1. Make sure you have Python in your PATH.确保你的 PATH 中有 Python。
  2. Find the Matlab root folder.找到 Matlab 根文件夹。 You can use the matlabroot command within Matlab to find it.您可以使用Matlab中的matlabroot命令来查找它。
  3. Go to the Matlab root folder in the command line.在命令行中转到 Matlab 根文件夹。
  4. cd "matlabroot\extern\engines\python" (In Windows) cd "matlabroot\extern\engines\python" (在 Windows 中)
  5. python setup.py install

I was stuck on this for so long and I cant find a good explanation for it so here y'all go.我被困了这么久,我找不到很好的解释,所以你们都去吧。 There's a package for python called matlab here这里有一个名为 matlab 的 python 包

And it has nothing to do with the matlab engine for python.它与python的matlab引擎无关。 When you pip install matlab it's installing this.当您 pip install matlab 它正在安装它。 I'm using pycharm and this is the default one it installed.我正在使用 pycharm,这是它安装的默认设置。 I uninstalled this matlab and, instead, I copied the necessary information to my python project.我卸载了这个 matlab,而是将必要的信息复制到了我的 python 项目中。

To do this, I located the folder named Matlab that is copied somewhere in AppData when you run python setup.py install in matlabroot/extern/engines/python and copied it to the lib folder in the venv of my python project since I'm using the virtual environment interpreter for pycharm.为此,我找到了名为 Matlab 的文件夹,当您在 matlabroot/extern/engines/python 中运行 python setup.py install 时,该文件夹被复制到 AppData 中的某个位置,并将其复制到我的 python 项目的 venv 中的 lib 文件夹中,因为我是使用 pycharm 的虚拟环境解释器。

pip install matlab gives you this , which installs a module with pip install matlab给你这个,它安装一个模块

from numpy import *
from pylab import *

inside.里面。 I'm quite sure this is not what you wanted...我很确定这不是你想要的...

I guess you tried importing the Matlab Compiler Runtime for Python.我猜你尝试过为 Python 导入 Matlab 编译器运行时。 This has to be installed with the respective software from Mathworks though, it doesn't come through Python package index / pip.这必须与 Mathworks 的相应软件一起安装,但它不是通过 Python 包索引/pip 来安装的。 Check out the instrucionson their site .查看他们网站上的说明

You can visit to MATLAB official documentation: https://www.mathworks.com/help/matlab/matlab_external/install-the-matlab-engine-for-python.html可以访问MATLAB官方文档: https ://www.mathworks.com/help/matlab/matlab_external/install-the-matlab-engine-for-python.html

I have copied here as well.我这里也复制了。

Install Python Engine for Multiple MATLAB Versions You can specify a MATLAB version to run from a Python script by installing the MATLAB Python packages to version-specific locations.为多个 MATLAB 版本安装 Python 引擎您可以通过将 MATLAB Python 包安装到特定于版本的位置来指定要从 Python 脚本运行的 MATLAB 版本。 For example, suppose that you want to call either MATLAB R2019a or R2019b from a Python version 3.6 script.例如,假设您想从 Python 3.6 版脚本调用 MATLAB R2019a 或 R2019b。

From the Windows system prompt, install the R2019a package in a subfolder named matlab19aPy36:在 Windows 系统提示符下,将 R2019a 包安装到名为 matlab19aPy36 的子文件夹中:

cd "c:\Program Files\MATLAB\R2019a\extern\engines\python" 
python setup.py install --prefix="c:\work\matlab19aPy36"

Install the R2019b package in a matlab19bPy36 subfolder:在 matlab19bPy36 子文件夹中安装 R2019b 包:

cd "c:\Program Files\MATLAB\R2019b\extern\engines\python" 
python setup.py install --prefix="c:\work\matlab19bPy36"

From a Linux system prompt:在 Linux 系统提示符下:

cd "/usr/local/MATLAB/R2019a/bin/matlab/extern/engines/python"
python setup.py install --prefix="/local/work/matlab19aPy36"
cd "/usr/local/MATLAB/R2019b/bin/matlab/extern/engines/python"
python setup.py install --prefix="/local/work/matlab19bPy36"

From a Mac Terminal:从 Mac 终端:

cd "/Applications/MATLAB_R2019a.app/extern/engines/python"
python setup.py install --prefix="/local/work/matlab19aPy36"
cd "/Applications/MATLAB_R2019b.app/extern/engines/python"
python setup.py install --prefix="/local/work/matlab19bPy36"

I too did the same.我也这样做了。 Installed matlab using使用安装的matlab

pip install matlab pip 安装 matlab

and got the same error No module named 'matlab.engine'; 'matlab' is not a package.并得到相同的错误No module named 'matlab.engine'; 'matlab' is not a package. No module named 'matlab.engine'; 'matlab' is not a package.

Then I checked the official documentation for installing MATLAB Engine API for Python , and followed the installation steps from there.然后我查看了安装MATLAB Engine API for Python的官方文档,并按照那里的安装步骤进行操作。

For me,为了我,

pip install matlabengine pip 安装 matlabengine

solved the issue!解决了问题!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 导入错误:没有名为 matlab.engine 的模块 - import error : no module named matlab.engine 如何使用matlab.engine在Matlab和python之间传递变量 - How to pass variables between Matlab and python using matlab.engine ImportError:没有名为matlab.engine的模块。 如何在Pycharm中导入matlab.engine? - ImportError: No module named matlab.engine. How to import matlab.engine in Pycharm? 为什么matlab.engine for python这么慢? - Why matlab.engine for python is so slow? 使用 Matlab.engine 同时安装 tensorflow - Using Matlab.engine and installing tensorflow at the same time How to call a Matlab function in Python from matlab.engine using the user's input (variable of type string) as the name of the function? - How to call a Matlab function in Python from matlab.engine using the user's input (variable of type string) as the name of the function? 问题 Pycharm 使用 matlab 引擎:ModuleNotFoundError: No module named 'matlab' - Problem with Pycharm using matlab engine: ModuleNotFoundError: No module named 'matlab' matlab.engine 的导入在 IPython 中有效,但在 Jupyter 中无效 - Import of matlab.engine works in IPython but not in Jupyter Python Matlab引擎错误 - Python Matlab Engine Error 是否可以从 Google Cloud 上的 Flask webapp 调用 matlab.engine ? - Is it possible to call matlab.engine from Flask webapp on Google Cloud?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM