主要内容

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:

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:

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 SystemProcedure
Windows®
  1. Run C:\Windows\System32\SystemPropertiesAdvanced.exe and click Environment Variables.

  2. Select the system variable you want to set and click Edit.

    If you do not have administrator rights on the machine you are using, select the user variable of the same name instead of the system variable.

  3. Click New and add the appropriate path. For example, for the PYTHONHOME environment variable, add your Python installation folder, such as C:\Program Files\Python<version>\.

  4. Click OK to apply the change.

Linux®

In a Bash shell, enter the following commands:

echo "export <ENVIRONMENT_VARIABLE>=<path_to_Python_install>" >> ~/.bash_profile
source ~/.bash_profile
macOS (Mojave 10.14 or earlier)

In a Bash shell, enter the following commands:

echo "export <ENVIRONMENT_VARIABLE>=<path_to_Python_install>" >> ~/.profile
source ~/.profile
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" >> ~/.profile
macOS (Catalina 10.15 or later)

In a Zsh shell, enter the following commands:

echo "export <ENVIRONMENT_VARIABLE>=<path_to_Python_install>" >> ~/.zprofile
source ~/.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.

See Also

Topics