
How to install Python environment and call it from MATLAB?
15 views (last 30 days)
Show older comments
MathWorks Support Team
on 19 Nov 2025 at 0:00
Answered: MathWorks Support Team
on 19 Nov 2025 at 22:03
Can you demonstrate a procedure to call Python scripts from MATLAB?
Accepted Answer
MathWorks Support Team
on 19 Nov 2025 at 0:00
1) Supported versions of Cpython
The "Cpython" is the recommended Python environment. Make sure that Cpython has continuously been upgraded and your environment is determined depending on your MATLAB version.
https://www.mathworks.com/support/requirements/python-compatibility.html
2) Installation of CPython
The Cpython is downloadable from the below site. Note that not every version/revision is available.
https://www.python.org/downloads/windows/
In the way installing, you will see the below window. Also be noted that the CPython will be installed in the below default directory. If you don't prefer the place, select "Customized installation" to select such in C:\Program Files .
C:\Users\<username>\AppData\Local\Programs\Python

Finally after installation, configure the below two folders (Python310 & Scripts) in the Path of Environment Variable:
python.exe: C:\Users\<username>\AppData\Local\Programs\Python\Python310
pip.exe: C:\Users\<username>\AppData\Local\Programs\Python\Python310\Scripts
After the path configuration, you will be able to execute Python program and MATLAB acknowledges existence of Python.
Point: MATLAB requires restarting after the installation. pyenv retunes a response if the CPython is correctly installed:
>> pyenv
ans =
PythonEnvironment with properties:
Version: "3.10"
Executable: "C:\Program Files\Python3.10.10\python.EXE"
Library: "C:\Program Files\Python3.10.10\python310.dll"
Home: "C:\Program Files\Python3.10.10"
Status: NotLoaded
ExecutionMode: InProcess
3) Run sample Python code on MATLAB.
>> pyrun(["greeting = 'hello'", "print(greeting)"])
hello
>> str = pyrun("greeting = 'hello'", "greeting")
str =
Python str with no properties.
hello
If you wish to run external Python script files (... .py), use pyrunfile command instead of pyrun.
test.py
------------------------
print('Hello World')
import math
print(math.sqrt(9))
>> pyrunfile('test.py')
Hello World
3.0
4) Installing Python libraries
pip.exe works to install external Python libraries such as NumPy. Open Windows' Command Prompt and type as follows:
> pip install numpy
Caution: It is not realistic for installing two versions of CPython as it often interfers unless you well know about the path control. If you need to upgrade CPython along with MATLAB update, uninstall the old CPython to remove concerns.
0 Comments
More Answers (0)
See Also
Categories
Find more on Call Python from MATLAB in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!