py_addpath(directory, MATLAB_too)
Editor's Note: This file was selected as MATLAB Central Pick of the Week
py_addpath works like the MATLAB function addpath, but updates the PYTHONPATH instead of the MATLAB path.
Starting in MATLAB 2014b, Python code can be called directly from MATLAB (assuming you have a Python interpreter and it is known to MATLAB). For example, py.statistics.mean([1, 2, 3]) would call the mean function from the statistics module in the Python standard library and return the mean of the array.
To use a function in a Python module from MATLAB, that module must be on the Python search path. Adding its location to the MATLAB search path won't work.
Modules in the standard library or properly installed third party packages are already on the Python search path. But if you have written your own Python functions, you may want to add their location to the search path in the same way you add the location of MATLAB functions and scripts to the MATLAB search path.
py_addpath adds a directory to the Python search path in the same way that the MATLAB function addpath adds a directory to the MATLAB search path. Like addpath, py_addpath only adds the directory for the currently active session: it does not permanently change the PYTHONPATH.
The optional output of py_addpath is the updated list of directories on the Python search path as a cell array.
Cite As
Eric Fields (2024). py_addpath(directory, MATLAB_too) (https://www.mathworks.com/matlabcentral/fileexchange/62703-py_addpath-directory-matlab_too), MATLAB Central File Exchange. Retrieved .
MATLAB Release Compatibility
Platform Compatibility
Windows macOS LinuxCategories
- MATLAB > Programming > Files and Folders > Search Path >
- MATLAB > External Language Interfaces > Python with MATLAB > Call Python from MATLAB >
Tags
Acknowledgements
Inspired: zipToolsPy
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!Discover Live Editor
Create scripts with code, output, and formatted text in a single executable document.