py_addpath(director​y, MATLAB_too)

Modify Python search path from MATLAB
489 Downloads
Updated 9 Nov 2017

View License

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
Created with R2014b
Compatible with any release
Platform Compatibility
Windows macOS Linux
Acknowledgements

Inspired: zipToolsPy

Community Treasure Hunt

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

Start Hunting!
Version Published Release Notes
1.2.0.0

Fixed description formatting
Now adds the full path even when the input is a relative path.

1.1.0.0

Added informative error messages.

1.0.0.0

Fixed typos in description.