How do I change the Python version used by "mwpython"?

5 Ansichten (letzte 30 Tage)
I was following the example on how to make a MATLAB Compiler SDK Python package on Mac OS X on the following documentation page:
On macOS, I must use the "mwpython" script at "/Applications/MATLAB_R2020a.app/bin/mwpython", but here is where I ran into an issue. When I executed the "mwpython" command in my terminal, it seemed to invoke Python 2.7, when I had used Python 3.7 to create my Python package.
How can I change "mwpython", so that it will invoke Python 3.7 instead of Python 2.7?

Akzeptierte Antwort

MathWorks Support Team
MathWorks Support Team am 31 Aug. 2021
The reason that "mwpython" invokes Python 2.7 is because this is the default Python installation provided on Mac computers.
If you have a different version of Python that you would like "mwpython" to invoke, then you will need to set the "PYTHONHOME" environment variable. Specifically, the "PYTHONHOME" environment variable should be set to the Python installation folder for the desired version of Python.
The best way to get started with this process is to use the "pyenv" command in the MATLAB Command Window to display Python installation information. Refer to the following documentation for more information on the "pyenv" command:
As an example, execute the following "pyenv" command using the path to a Python 3.7 executable ("/usr/bin/python3") to set up the Python environment in a MATLAB session:
>> pyenv("Version", "/usr/bin/python3")
ans =
PythonEnvironment with properties:
Version: "3.7"
Executable: "/Applications/Xcode11.3.1.app/Contents/Developer/usr/bin/python3"
Library: "/Applications/Xcode11.3.1.app/Contents/Developer/Library/Frameworks/Python3.framework/Versions/3.7/lib/libpython3.7m.dylib"
Home: "/Applications/Xcode11.3.1.app/Contents/Developer/Library/Frameworks/Python3.framework/Versions/3.7"
Status: NotLoaded
ExecutionMode: InProcess
This command sets up the Python environment in MATLAB to use Python 3.7, which can be seen in the "Version" property. If this property is not set to the value that you expect, then you may have the wrong version of Python installed.
The reason that using "pyenv" is important is that the "Home" property of the output of the "pyenv" command provides the path that the "PYTHONHOME" environment variable should point to. This will not necessarily be the same path that is shown in the example above. 
The next step is to set the "PYTHONHOME" environment variable. This will depend on which type of shell your terminal is running. The following examples show what to run in your terminal if you are using either a zsh or bash shell. <python installation directory> should be replaced with the directory given by the "Home" property of the output of the "pyenv" command in the MATLAB Command Window.
For the zsh shell, use the following commands in your terminal:
echo 'export PYTHONHOME=<python installation directory>' >> ~/.zshenv
source ~/.zshrc
For the bash shell, use the following commands in your terminal:
echo 'export PYTHONHOME=<python installation directory>' >> ~/.bash_profile
source ~/.bash_profile
For example, if you are using the "pyenv" example above with a bash shell, you would use the following commands in your terminal:
echo 'export PYTHONHOME=/Applications/Xcode11.3.1.app/Contents/Developer/Library/Frameworks/Python3.framework/Versions/3.7' >> ~/.bash_profile
source ~/.bash_profile
Once the correct set of commands has been executed in your terminal, "mwpython" will invoke the correct version of MATLAB. 
This may be the final result of executing "/Applications/MATLAB_R2021a.app/bin/mwpython" in a terminal:
$ /Applications/MATLAB_R2021a.app/bin/mwpython
Python 3.7.3 (default, Dec 13 2019, 19:58:14)
[Clang 11.0.0 (clang-1100.0.33.17)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>>

Weitere Antworten (0)

Kategorien

Mehr zu Call Python from MATLAB finden Sie in Help Center und File Exchange

Produkte


Version

R2021a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by