MATLAB Answers

Changing Python Environment used by MATLAB

89 views (last 30 days)
Furqan Hashim
Furqan Hashim on 9 Aug 2020
Edited: Gabriel Bergqvist on 30 Aug 2021
I have mutiple python enviroments on a windows server. I need to change the Python enviroment that MATLAB is using. MATLAB version that I am using is 2017b and I've used the command pyversion('/path/to/python/venv') to change python environment being used by MATLAB. This command gives an error
Python is loaded. The version cannot be changed in this MATLAB session. To change the version, restart MATLAB, and then call 'pyversion'.
To deal with the above error I've made changes to startup.m file by changing two variables
pyPath = '/path/to/python/venv';
pyLibPath = [pyPath 'Lib\site-packages\projects'];
After making changes and restarting MATLAB I get an error
Unable to set Python environment to /path/to/python/venv in this session.
Please restart MATLAB!
I suspect that if results from command py.sys.path contains any path with directory that refers to old python environment I won't be able to set new Python environment. E.g. if the result from command py.sys.path is
['/path/to/python/venv', '','/path/to/python/old_venv']
I need to remove '/path/to/python/old_venv' using remove(py.sys.path, '/path/to/python/old_venv'). I did that but when I restart MATLAB after doing that I see that output from py.sys.path does not change as it still contains '/path/to/python/old_venv'. To be sure that older python paths are removed from py.sys.path I've ran following commands in new Python environment
import sys
Output from above doesnot contain any paths from older Python environment.
Can someone provide a solution on how can I change the Python environment being used by MATLAb?
I've mentioned all the things that I've tried.
  1 Comment
Arturo Pino
Arturo Pino on 4 Aug 2021
I'm dealing with this exact same issue. After initially setting a python environment, I can't reset even after restarting matlab because it seems to default to the environment I gave it several sessions ago. No matter what I do, it always will load the previously loaded environment and tells me to restart matlab because the environment is already set. So frustrating!

Sign in to comment.

Answers (1)

Yongjian Feng
Yongjian Feng on 11 Aug 2021
virtualenv is challenging. Is it possible to avoid virtualenv?
Gabriel Bergqvist
Gabriel Bergqvist on 30 Aug 2021
One more thing, seemingly unrelated actions may load the pyenv for you. For example, setting a breakpoint may or may not load the pyenv, depending on exactly in which file you set it (typically setting it in Matlab's internal read-only files may trig this, e.g. getPropertyGroups.m). This makes trying to understand exactly when the pyenv is loaded quite difficult, since the action of setting breakpoints itself may load pyenv. In short, assume pyenv will be loaded at any point during startup of AppDesigner, your App, or the debugger for that matter.
Hence, you may need to explicitly load your preferred pyenv (with whatever config you need) before starting things like AppDesigner, your App, or similar.
From a developer's perspective, I think this should be considered a bug.
I also suspect that certain ways of calling Matlab code may trig the pyenv loading as well. For example, running some commands as a .m function file may load pyenv prematurely (i.e. break the execution) while the same code executed as a .m script file always works (script file = without a function declaration). There is determinism to when Matlab choses to freeze your pyenv for you, albeit absolutely zero transparency or apparent logic. This makes it very hard to use the pyenv functionality in Matlab for more advanced use-cases, unfortunately. It is possible, but bordering reverse-engineering.

Sign in to comment.




Community Treasure Hunt

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

Start Hunting!