Effacer les filtres
Effacer les filtres

Python virtual environments with MATLAB

489 vues (au cours des 30 derniers jours)
MathWorks Support Team
MathWorks Support Team le 29 Juin 2022
How can I use virtual environments with MATLAB's Python Interface?

Réponse acceptée

MathWorks Support Team
MathWorks Support Team le 31 Jan 2023
Modifié(e) : MathWorks Support Team le 31 Jan 2023
This document contains instructions for using a Python virtual environment, which can be used with MATLAB's Python Interface, the MATLAB Engine API for Python, MATLAB Compiler SDK for Python, or the MATLAB Production Server Python Client. This example demonstrates its use with MATLAB's Python Interface, but except where indicated, the content applies to the others as well. These instructions are OS-dependent. 
In this example, python.exe is located in the following directory:
C:\Users\username\AppData\Local\Programs\Python
Pass the --version flag to the Python interpreter to determine its version, if necessary.  
C:\Users\username>  C:\Users\username\AppData\Local\Programs\Python\python --version 
1) Create a virtual environment
Create a virtual environment named py38 under C:\users\username. 
C:\Users\username>  C:\Users\username\AppData\Local\Programs\Python\python -m venv C:\Users\username\py38 
2)  Activate the virtual environment
C:\Users\username> C:\Users\username\py38\Scripts\activate.bat 
Once activated, you should see the Python virtual environment name (py38) at the start of the command prompt.
(py38) C:\Users\username>
3) Install packages 
Install any Python packages that are required for your project. For example, to install version 3.1 of h5py, execute the following command. 
(py38) C:\Users\username> python -m pip install h5py==3.1 
Any packages installed while a virtual environment is activated will only be accessible from that virtual environment. 
4) Verify that modules can be loaded in Python
Start Python
(py38) C:\Users\username> python
>>> import h5py 
5) Find the location of the Python executable in the virtual environment 
(py38) C:\Users\username$ python 
>>> import sys 
>>> sys.executable 
In this case, a symbolic link to the Python executable is located at the following path:
C:\Users\username\py38\Scripts\python.exe
Start MATLAB from the Command Prompt window or using the Start menu.
1) Set the Python environment (applies to Python Interface only)
Set the Python environment to match the location of the Python executable in the virtual environment. 
Using the same Python 3.8 example as above, and changing the execution mode to OutOfProcess to avoid library conflicts: 
>> pyenv('Version', ...
'C:\Users\username\py38\Scripts\python', ...
'ExecutionMode','OutOfProcess')
ans =
PythonEnvironment with properties:
Version: "3.8"
Executable: "C:\Users\username\py38\Scripts\python.EXE"
Library: "C:\Users\gmkep\AppData\Local\Programs\Python\Python38\python38.dll"
Home: "C:\Users\username\py38"
Status: NotLoaded
ExecutionMode: OutOfProcess
2) Use the library (applies to Python Interface only)
In this example, we will load the library.
>> py.importlib.import_module('h5py')
ans =
Python module with properties:
bartlett: [1×1 py.function]
float32: [1×1 py.type]
right_shift: [1×1 py.numpy.ufunc]
isnan: [1×1 py.numpy.ufunc]
...
 
In this example, python3 is located in the following location:  
/usr/bin/python3 
Pass the --version flag to the Python interpreter to determine its version. 
/home/username$ /usr/bin/python3 --version 
Python 3.8.2 
1) Create a virtual environment
Create a virtual environment named py38 under /home/username. 
/home/username/usr/bin/python3 -m venv /home/username/py38 
2) Activate the virtual environment
The following example assumes that the bash shell is being used. For the csh or tcsh shell, replace "activate" by "activate.csh"
/home/username$ source /home/username/py38/bin/activate
Once activated, you should see the Python virtual environment name (py38) at the start of the command prompt.
(py38) /home/username$
3) Install packages
Install any Python packages that are required for your project. For example, to install version 3.1 of h5py, execute the following command. 
(py38) /home/username$ python -m pip install h5py==3.1 
4) Verify that module can be loaded in Python
Start Python and import the module.
(py38) /home/username$ python
>>> import h5py
5) Find the location of the Python executable in the virtual environment
One way to find the location of the executable is to start Python in the virtual environment terminal, and execute the following commands.
(py38) /home/username$ python
>>> import sys
>>> sys.executable
In this case, a symbolic link to the Python executable is located at the following path. 
/home/username/py38/bin/python 
1) Start MATLAB from the terminal
(py38) /home/username$ matlab
2)  Set the Python environment (applies to Python Interface only)
Set the Python environment "Version" field to point to a symbolic link to the Python executable in the virtual environment. Using the same Python 3.8 example as above and changing the execution mode to OutOfProcess to avoid library conflicts: 
>> pyenv('Version', ...
'/home/username/py38/bin/python', ...
'ExecutionMode','OutOfProcess')
ans =
PythonEnvironment with properties:
Version: "3.8"
Executable: "/home/username/py38/bin/python"
Library: "<library path>/libpython3.8.so"
Home: "/home/username/py38"
Status: NotLoaded
ExecutionMode: OutOfProcess
3) Use the module (applies to Python Interface only)
In this example we will load the h5py module.
>> py.importlib.import_module('h5py')
ans =
Python module with properties:
get_enum: [1×1 py.builtin_function_or_method]
check_enum_dtype: [1×1 py.builtin_function_or_method]
Reference: [1×1 py.type]

Plus de réponses (0)

Catégories

En savoir plus sur Call Python from MATLAB dans Help Center et File Exchange

Tags

Aucun tag saisi pour le moment.

Community Treasure Hunt

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

Start Hunting!

Translated by