py_addpath(director​y, MATLAB_too)

Modify Python search path from MATLAB
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) (, MATLAB Central File Exchange. Retrieved .

MATLAB Release Compatibility
Created with R2014b
Compatible with any release
Platform Compatibility
Windows macOS Linux

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

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

Added informative error messages.

Fixed typos in description.