Configure Your Environment for Generating Python Packages
Before you can run Python® code or generate Python packages using MATLAB® Compiler SDK™, ensure that your Python development environment is properly configured by verifying the following:
Your development system uses a version of Python that is compatible with MATLAB. For updated Python system requirements, see MATLAB Interfaces to Other Languages.
Python is included in your system's
PATH.Your target machine has MATLAB or MATLAB Runtime installed. For more information on target machine requirements, see MATLAB Compiler SDK Python Target Requirements. For instructions on how to install MATLAB Runtime, see Download and Install MATLAB Runtime.
Install Supported Python Version
To integrate packaged MATLAB functions into your Python code, you must have a version of Python installed that is compatible with MATLAB Compiler SDK. Then, you can use a code editor to write a Python application that integrates the Python package generated from your MATLAB functions. You can install a compatible version of Python by following these steps:
Go to https://www.python.org/downloads/ and scroll to the section Looking for a specific release?
Find the version you want and click Download. For supported version information, see Versions of Python Compatible with MATLAB Products by Release.
Click the format you want for the 64-bit version and follow the online instructions.
Install Python in Virtual Environment
You can create a virtual environment for a specific version of Python using the
Python venv module. For venv instructions, see
the MATLAB
Answers™ article Python virtual environments with MATLAB. Note that, for the purpose of
compiling MATLAB code into Python packages using MATLAB
Compiler SDK, you do not have to complete the Setup steps in
MATLAB section. For information about virtual Python environments,
see the Python tutorial Virtual Environments
and Packages.
Set Environment Variables
Once you have installed a compatible Python version, set the relevant environment variables. This is necessary to
properly install Python packages created with MATLAB
Compiler SDK. From the command line, set the location of the standard Python libraries using the PYTHONHOME environment variable.
Similarly, set the default search path for module files, including MATLAB functions packaged using MATLAB
Compiler SDK, with the PYTHONPATH environment variable. To run
Python from the command line, add your Python installation to your PATH. For more information about
the PYTHONHOME environment variable, see PYTHONHOME. For more information about the PYTHONPATH
environment variable, see PYTHONPATH. If you are using a virtual environment, setting
PYTHONPATH is not necessary.
Note
Setting PYTHONHOME is distinct from setting a Python interpreter for MATLAB using pyenv, which
determines the Python environment used when you call Python from MATLAB.
The table below provides the steps to set the necessary environment variables using different operating systems.
| Operating System | Procedure |
|---|---|
| Windows® |
|
| Linux® | In a Bash shell, enter the following commands: echo "export <ENVIRONMENT_VARIABLE>=<path_to_Python_install>" >> ~/.bash_profilesource ~/.bash_profile |
| macOS (Mojave 10.14 or earlier) | In a Bash shell, enter the following commands: echo "export <ENVIRONMENT_VARIABLE>=<path_to_Python_install>" >> ~/.profilesource ~/.profile PATH, make sure to add to
PATH using the following command
instead:echo "export <ENVIRONMENT_VARIABLE>=<path_to_Python_install>:$PATH" >> ~/.profile |
| macOS (Catalina 10.15 or later) | In a Zsh shell, enter the following commands: echo "export <ENVIRONMENT_VARIABLE>=<path_to_Python_install>" >> ~/.zprofilesource ~/.zprofile In
the case of PATH, make sure to add to
PATH using the following command
instead:echo "export <ENVIRONMENT_VARIABLE>=<path_to_Python_install>:$PATH" >> ~/.zprofile |
Additionally, set the library path to locate your MATLAB Runtime libraries. This path is set automatically on Windows, but must be set manually on other platforms. For instructions, see Set MATLAB Runtime Library Paths for Deployment.
Run Packaged MATLAB Code in Python on macOS
If you are running your packaged MATLAB code on macOS, you need to use the mwpython command instead of the
standard python command.
For example, to run a Python file named main.py, run the following command in your
terminal:
mwpython main.py
For more information, including input arguments and MATLAB Runtime startup options, see mwpython.
Some development environments may use the python command to run
Python scripts by default. If this is the case, you must specify a launch
configuration that runs Python using mwpython. See debug configuration for information about debug configurations in Visual
Studio Code. See project configuration for information on project configurations in Anaconda.
See run/debug configuration for information about run/debug configurations in
PyCharm.