How can I execute Python code in a virtualenv from Matlab
You can either modify the PATH
environment variable in MATLAB prior to calling python from MATLAB
% Modify the system PATH so it finds the python executable in your venv first
setenv('PATH', ['/path/to/my/venv/bin', pathsep, getenv('PATH')])
% Call your python script
system('python myscript.py')
Or the better way would be to specify the full path to the python binary
system('/path/to/my/venv/bin/python myscript.py')
As suggested in comment by @tales-pádua you may use pyversion command to set path to Python executable you are using (before trying to call python from Matlab).
This can be automated by use of matlabrc.m file:
python = '.local/bin/python';
if exist(python, 'file')
pyversion(python)
end