Python virtual environments with MATLAB
이 질문을 팔로우합니다.
- 팔로우하는 게시물 피드에서 업데이트를 확인할 수 있습니다.
- 정보 수신 기본 설정에 따라 이메일을 받을 수 있습니다.
오류 발생
페이지가 변경되었기 때문에 동작을 완료할 수 없습니다. 업데이트된 상태를 보려면 페이지를 다시 불러오십시오.
이전 댓글 표시
1 개 추천
How can I use virtual environments with MATLAB's Python Interface?
채택된 답변
MathWorks Support Team
2026년 6월 16일 0:00
편집: MathWorks Support Team
2026년 6월 16일 18:36
3 개 추천
This document contains instructions for using a Python virtual environment, which can be used with these products: MATLAB Python Interface, the MATLAB Engine API for Python, MATLAB Compiler SDK for Python, or the MATLAB Production Server Python Client.
Starting in R2026a, it is recommended that you use the Python view in the External Languages panel to manage virtual environments that you will use with MATLAB Python Interface or the MATLAB Engine API for Python. 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. For more details see: Manage Python Environments Using External Languages Panel.
If you are not using the Python panel, this example demonstrates how to use a virtual environment with MATLAB's Python Interface. Except where indicated, the content applies to the other products listed above. 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]
추가 답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 Python Package Integration에 대해 자세히 알아보기
제품
참고 항목
2022년 6월 29일
2026년 6월 16일 18:36
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!웹사이트 선택
번역된 콘텐츠를 보고 지역별 이벤트와 혜택을 살펴보려면 웹사이트를 선택하십시오. 현재 계신 지역에 따라 다음 웹사이트를 권장합니다:
또한 다음 목록에서 웹사이트를 선택하실 수도 있습니다.
사이트 성능 최적화 방법
최고의 사이트 성능을 위해 중국 사이트(중국어 또는 영어)를 선택하십시오. 현재 계신 지역에서는 다른 국가의 MathWorks 사이트 방문이 최적화되지 않았습니다.
미주
- América Latina (Español)
- Canada (English)
- United States (English)
유럽
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
