简体   繁体   中英

(/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. If that switch is on, I want Matlab to execute a python script through a system command as such:

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.

EDIT: To clarify, if I run 'python3 osx_conversion.py' through the terminal it runs smoothly

EDIT 2: Result from running

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.

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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