2

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
7
  • 1
    Well, do you have python3 installed by this name on the mac? Commented Dec 19, 2018 at 23:39
  • Yes, edited post accordingly Commented Dec 19, 2018 at 23:41
  • 1
    run echo $PATH in terminal and post the result here Commented Dec 19, 2018 at 23:43
  • Thank you! Posted result as an edit to the question Commented Dec 19, 2018 at 23:50
  • 1
    try to change cmdStr = 'python3 osx_conversion.py' to cmdStr = '/anaconda3/bin/python3 osx_conversion.py' (firstly, run /anaconda3/bin/python3 osx_conversion.py in terminal just to make sure it works) Commented Dec 19, 2018 at 23:54

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

2 Comments

No, MATLAB is not run as root, that would be dangerous. On MacOS, programs started from Finder (the desktop program) do not receive any environment variables or settings defined in .bashrc or .profile.
That makes more sense

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.