Hướng dẫn matlab python environment

This document contains instructions for using a Python virtual environment with MATLAB's Python Interface. These instructions are OS-dependent

  • Windows
    • Setup steps in Windows Command Prompt
    • Setup steps in MATLAB
  • Linux/MacOS
    • Setup steps in Linux/MacOS terminal
    • Setup steps in MATLAB

Note:  For releases R2021b and earlier, edit the Windows Path environmental variable. This step is necessary for Python versions 3.7+ when using MATLAB R2021b, or earlier. From a Windows Command Prompt, edit the PATH environmental variable to include the path to the Python executable. In this example, python.exe is located in the following location.

C:\Users\username\AppData\Local\Programs\Python\Python38

so the command would be as follows.

set PATH=C:\Users\username\AppData\Local\Programs\Python\Python38;

1) Create a virtual environment

Create a virtual environment named py38.

C:\Users\username> 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) Find the location of the Python executable in the virtual environment

In this case, the Python executable is located in the following directory.

C:\Users\username\py38\Scripts\python.exe

4) Install packages

Install Python any package 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

5) Verify that modules can be loaded in Python

Start Python

(py38) C:\Users\uername> python

>>> import numpy

Start MATLAB from the Command Prompt window or using the Start menu.

1) Set the Python environment

Set the Python environment to match the location of the Python executable in the virtual environment.

The best practice is to use an OutOfProcess execution mode to avoid library conflicts.  Using the same Python 3.8 example, as above,

>> 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. In this example, we will load the library.

>> py.importlib.import_module('numpy')

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]

...

1) Create a virtual environment

Create a virtual environment named py38.

/home/username$ python -m venv /home/username/py38

2) Activate the virtual environment

/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.

3) Install packages

Install Python any package 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.exec_prefix

In this case, the Python executable is located in the following directory.

1) Start MATLAB from the terminal.

(py38) /home/username$ matlab

(py38) /home/username$ matlab

2)  Set the Python environment**

Set the Python environment to match the location of the Python executable in the virtual environment. The best practice is to use an OutOfProcess execution mode to avoid library conflicts. Using the same Python 3.8 example, as above,

>> pyenv('Version', ...

'/home/username/py38/bin/python', ...

'ExecutionMode','OutOfProcess')

ans =

PythonEnvironment with properties:

Version: "3.8"

Executable: "/home/username/py38/bin/python"

Library: "/libpython3.8.so"

Home: "/home/username/py38"

Status: NotLoaded

ExecutionMode: OutOfProcess

3) Use the module. 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]