简体   繁体   English

如何使用带有Matlab系统功能的Python处理“没有名为library_name的模块”错误?

[英]How do I deal with 'no module named library_name' error using Python with Matlab's system function?

When I try to run a Python script in Matlab using the system command, it works find until I import something that doesn't come with base Python. 当我尝试使用system命令在Matlab中运行Python脚本时,它会一直有效,直到我导入基本Python所没有的东西。

For example, if I have a script test_1.py: 例如,如果我有一个脚本test_1.py:

import math as m
print 'good to go'
print m.exp(7)

and I run the Matlab command: 然后运行Matlab命令:

>>> system('python test_1.py')

I get back 我回来

good to go
1096.63315843

ans = 

    0

which is what I want. 这就是我想要的。 But if I modify test_1.py to be 但是如果我将test_1.py修改为

import math as m
import pandas as pd
print 'good to go'
print m.exp(7)

and run 并运行

>>> system('python test_1.py')

I get: 我得到:

>> system('python test_1.py')
Traceback (most recent call last):
  File "test_1.py", line 1, in <module>
    import pandas as pd
ImportError: No module named pandas

ans =

     1

Both scripts run fine when I run them in my Bash terminal and the problem doesn't seem to be specific to the Pandas library. 当我在Bash终端中运行它们时,这两个脚本都可以正常运行,而且问题似乎并不特定于Pandas库。

I checked Matlab's documentation and some other StackOverflow questions but I can't find anything closely related to my issue. 我检查了Matlab的文档和其他一些StackOverflow问题,但找不到与我的问题密切相关的任何内容。

When installing Anaconda with default settings, it will automatically add the Anaconda binaries to your $PATH variable. 使用默认设置安装Anaconda时,它将自动将Anaconda二进制文件添加到$PATH变量中。 If you choose not to, you can add it manually by adding the following line to your .bashrc (or .bash_profile on Mac) file: 如果选择不这样做,则可以通过.bashrc下行添加到.bashrc文件(在Mac上为.bash_profile文件)中来手动添加:

export PATH="/home/username/anaconda/bin:$PATH" 

Now, in your case, this has happened (in either way), so Anaconda does work in your bash shell. 现在,就您而言,这已经发生(无论哪种方式),因此Anaconda确实可以在bash shell中工作。 On Mac OS X, however, there are two different $PATH variables: the bash path, and the launchctl path. 但是,在Mac OS X上,有两个不同的$PATH变量:bash路径和launchctl路径。 The bash path is used if you open a terminal, or run a program from the terminal. 如果您打开终端或从终端运行程序,则使用bash路径。 The launchctl path is used when you run a program "the normal way", which is with the launchctl command. 当您以“ launchctl命令“正常方式”运行程序时,将使用launchctl路径。 Now, your bash path is correct, but your launchctl path isn't - that is why it works in a terminal, but not from MATLAB. 现在,您的bash路径是正确的,但您的launchctl路径却不正确-这就是为什么它可以在终端中使用的原因,而不能在MATLAB中使用。

You can either set the launchctl path to be the same as the bash path, by adding the following line to your .bash_profile : 您可以通过launchctl添加到.bash_profile ,来将launchctl路径设置为与bash路径相同:

launchctl setenv PATH $PATH

This changes the path settings for all programs, which might be undesirable. 这将更改所有程序的路径设置,这可能是不希望的。 So, it might be easiest and safest to add the Anaconda binaries to the path from within MATLAB with the path function, at the start of your script: 因此,在脚本开始时,使用path函数从MATLAB中将Anaconda二进制文件添加到路径中可能是最简单,最安全的:

path('Users/JackStClaire/anaconda/bin/', path)

By adding it to the beginning of your $PATH variable, which makes sure that it uses the Anaconda python and not the system python . 通过将其添加到$PATH变量的开头,可以确保它使用Anaconda python而不是system python

In the Matlab script you can simply set the environment that Python use to communicate with its libraries to be sure that it is the right one 在Matlab脚本中,您可以简单地设置Python用于与其库进行通信的环境,以确保它是正确的环境。

PATH_PYTHON = '/Users/matteofabris/anaconda2/lib/python2.7/site-packages/';
setenv('PYTHONPATH', PATH_PYTHON);

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

相关问题 如何解决以下Python错误:没有名为Playback的模块? - How do i fix the following Python error: no module named Playback? 如何使用 Python 中的 WMI 模块更改计算机的名称 - How do I change a computer's name with the WMI module in Python 如何将代码从库放入我的项目而不导入&#39;library_name&#39; - How to place code from library to my project without import 'library_name' 使用Python的inspect库来检查是否存在动态命名的函数 - Using Python's inspect library to check if a dynamically named function exists 使用 Python 运行 Matlab 会出现“No module named matlab.engine”错误 - Running Matlab using Python gives 'No module named matlab.engine' error Python commpy库错误:没有名为“过滤器”的模块 - Python commpy library error: no module named 'filters' 如何指定从 MATLAB 系统 function 运行的 Python 的版本? - How can I specify the version of Python that is run from MATLAB's system function? 如何成功处理OpenGL的gluLookAt函数? - How do I deal with OpenGL's function of gluLookAt successfully? 如何解决 Google Cloud Function 上的“No module named 'frontend'”错误消息 - How do I resolve "No module named 'frontend'" error message on Google Cloud Function 我该如何处理此回溯名称错误? 这是我的第一个剧本 - How do i deal with this traceback name error? This is my first script
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM