简体   繁体   English

(/ bin / bash:python3:找不到命令)在Matlab中通过系统命令运行python脚本时

[英](/bin/bash: python3: command not found) When running python script through system command in Matlab

I have a large Matlab project, and due to an issue with xlsread and Mac OS, I have included a Mac switch. 我有一个大型的Matlab项目,由于xlsread和Mac OS的问题,我包括了Mac开关。 If that switch is on, I want Matlab to execute a python script through a system command as such: 如果该开关打开,我希望Matlab通过系统命令执行python脚本,如下所示:

cmdStr = 'python3 osx_conversion.py'
if osxSwitch == 1
    [status,result] = system(cmdStr);
    if status ~= 0
        error('System could not run Python conversion file')
    end
else
...

This returns the result: 这将返回结果:

/bin/bash: python3: command not found

Now, if instead I use 'python osx_conversion.py', the error I get is to do with using python 3 syntax, as the command python on my mac calls python 2. Any ideas as to what is going on will be really appreciated. 现在,如果我改为使用“ python osx_conversion.py”,则我得到的错误是与使用python 3语法有关的,因为我的mac上的python命令将其称为python 2。

EDIT: To clarify, if I run 'python3 osx_conversion.py' through the terminal it runs smoothly 编辑:澄清一下,如果我通过终端运行“ python3 osx_conversion.py”,它将顺利运行

EDIT 2: Result from running 编辑2:运行结果

echo $PATH

in the terminal: 在终端:

    /anaconda3/bin:/Library/Frameworks/Python.framework/Versions/3.6/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Applications/VMware Fusion.app/Contents/Public:/Library/TeX/texbin:/usr/local/go/bin:/usr/local/MacGPG2/bin:/opt/X11/bin:/opt/ImageMagick/bin:/Applications/Wireshark.app/Contents/MacOS:/usr/local/go/bin

Your problem is that the command run by Matlab wasn't finding your python 3 interpreter, my guess is that it runs as root user, so the PATH is different than the one in your user. 您的问题是Matlab运行的命令找不到您的python 3解释器,我猜它是作为root用户运行的,因此PATH与您的用户中的PATH不同。

Basically, what you can do is specify the complete PATH for your desired Python interpreter, in your case, it is /anaconda3/bin/python3 , but that PATH might be different for another user. 基本上,您可以为所需的Python解释器指定完整的PATH,在您的情况下为/anaconda3/bin/python3 ,但该路径对于另一个用户可能有所不同。

In any case, you can always run whereis python3 in terminal to find out the complete PATH of the user Python interpreter, and use the complete PATH inside Matlab. 无论如何,您始终可以在终端中运行whereis python3来查找用户Python解释器的完整PATH,并在Matlab中使用完整的PATH。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM