主要内容

Manage Python Environments Using External Languages Panel

You can use the Python® view in the External Languages panel to configure and maintain your Python environments directly from MATLAB®. In the panel, you can switch Python environments, add or create environments, manage and share libraries, change the execution mode, and remove or delete environments.

To open the Python view in the External Languages panel, click the Open more panels button on any sidebar and click External Languages. Then select the Python option from the language list at the upper left of the panel.

A screenshot of the External Languages panel in MATLAB showing Python environments. The selected Python environment is version 3.12 located at "C:\Users\user\AppData\Local\Programs\Python\Python312\python.EXE". Under "All Python Environments," two base environments are listed: Python 3.12 and Python 3.11, each with their respective file paths. There is also a "Virtual Environments" section showing an environment named "sandbox01 - 3.12" located at "C:\Users\user\sandbox01\Scripts\pythonw.exe".

Switch Python Environments

The Python view within the External Languages panel displays your currently selected Python environment and other available Python environments, including base environments and virtual environments. You can change the selected Python environment by right-clicking an environment in the All Python Environments section and selecting Set as Selected Environment.

If you do not see an environment that you expect to see in the list, then add the environment by following the steps in the next section. If you installed a Python base environment with the MATLAB window open, you can also click the Refresh button to update the All Python Environments section. If a Python environment is not on the system path, MATLAB might not detect it.

A screenshot of the External Languages panel in MATLAB showing Python environments with a context menu open after right-clicking on the "sandbox01 - 3.12" virtual environment. The menu options are: "Set as Selected Environment," "Manage Python Libraries," "Generate Requirements," "Remove Environment," and "Delete Environment." The cursor is hovering over "Set as Selected Environment."

Add or Create Python Environments

If you want to configure an environment that is not listed in the Python view of the External Languages panel, you can either add an existing environment or create a new virtual environment in MATLAB.

Add Existing Environment

To add a base or virtual Python environment, click the Add button and select Add Environment. In the Add Environment window, type or browse to the location of the Python executable and choose whether to set it as the selected environment.

A window titled "Add Environment" with the instruction "Add an existing Python environment." There is a text field labeled "Python executable:" with a folder icon button next to it for browsing. Below is a checkbox labeled "Set as selected environment." At the bottom right, there are two buttons: "Add" (grayed out) and "Cancel."

Create Virtual Environment

To create a virtual Python environment, click the Add button and select Create Environment. In the Create Virtual Environment window, specify a base environment, name, location, libraries to install, and whether to set the virtual environment as the selected environment. MATLAB uses the Python venv module to create virtual environments.

A window titled "Create Virtual Environment." It contains dropdown and text fields for selecting a base environment, entering a name, and specifying a location. There are radio buttons to choose library installation options: "None," "All from base environment," or "From requirements file" (with an additional input field for the requirements file). There is also a checkbox labeled "Set as selected environment." At the bottom, there are two buttons: "Create" (disabled) and "Cancel." Small folder icons appear next to the location and requirements file fields, allowing the user to browse for a file rather than type in a path.

Manage and Share Libraries

Code written in Python often requires external libraries. You can install, update, or remove Python libraries within your environments, and share library configurations between environments.

Manage Libraries

To manage libraries in an existing Python environment, right-click the environment in the All Python Environments section and select Manage Python Libraries. From the Manage Environment window, you can add libraries to the environment, update installed libraries, or uninstall libraries. MATLAB uses the Python pip installer to add, update, or remove libraries. This installer affects not only your MATLAB environment but also any part of your system outside of MATLAB that uses the libraries.

The Add button in the Manage Environment window allows you to add a library using its name or a requirements.txt file. If you choose to add a library using its name, MATLAB installs the library from the Python Package Index (PyPI). If you previously generated a requirements.txt file, then you can use it to install libraries to a Python environment.

A window titled "Manage Environment" for managing Python libraries. It allows the user to install, update, or uninstall libraries in a selected environment. The selected environment is shown as "(sandbox01 - 3.12) C:\Users\user\sandbox01\Scripts\pythonw.exe". Below, there is a list of installed libraries: "numpy - (2.3.4)" and "pip - (24.2)", each with a checkbox. There are buttons for "Add", "Update", and "Uninstall" above the library list, with only "Add" enabled. A "Close" button is at the bottom right.

Replicate Environments

If you want to use the same libraries for another Python environment, you can generate a requirements.txt file from the original environment by right-clicking it in the All Python Environments list and selecting Generate Requirements. The generated requirements.txt file contains information about the Python environment, including its libraries. Then use the requirements.txt file to install the same libraries in another environment.

Change the Execution Mode

Depending on the purpose of your Python code, you might want to run it in in-process or out-of-process execution mode. To change the execution mode of the selected Python environment, click the Settings button and select either InProcess or OutOfProcess.

Depending on the direction of the mode change and whether the environment is loaded, MATLAB might need to terminate the environment or restart. For more information, see pyenv.

Remove or Delete Environments

When you no longer need a Python environment, you can remove it to reduce clutter. You can also delete virtual environments.

Remove Environment

You can remove a Python environment from the External Languages panel without deleting it. To remove an environment, right-click the environment in the All Python Environments section and select Remove Environment. When you remove an environment, it is recoverable. You can restore it to the Python view in the External Languages panel using the steps in Add Existing Environment.

Delete Virtual Environment

To permanently delete a virtual environment, right-click the environment in the All Python Environments section and select Delete Environment. This action deletes the virtual environment folder from the system and removes the environment from the External Languages panel. A deleted environment cannot be restored. You cannot delete base environments using the Python view in the External Languages panel.

See Also

Functions

Objects

Topics