[英]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.