MATLAB Answers

Changing Python Environment used by MATLAB

89 ビュー (過去 30 日間)
Furqan Hashim
Furqan Hashim 2020 年 8 月 9 日
編集済み: Gabriel Bergqvist 2021 年 8 月 30 日
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
sys.path
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 件のコメント
Arturo Pino
Arturo Pino 2021 年 8 月 4 日
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!

サインインしてコメントする。

回答 (1 件)

Yongjian Feng
Yongjian Feng 2021 年 8 月 11 日
virtualenv is challenging. Is it possible to avoid virtualenv?
  4 件のコメント
Gabriel Bergqvist
Gabriel Bergqvist 2021 年 8 月 30 日
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.

サインインしてコメントする。

製品


リリース

R2017b

Community Treasure Hunt

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

Start Hunting!

Translated by