Matlab requires restart before changes in Python code changes are applied

79 ビュー (過去 30 日間)
Daniel Stolzberg
Daniel Stolzberg 2020 年 8 月 7 日
回答済み: MONIKA 2024 年 1 月 3 日
Background:
I am running Matlab 2020a and Python 3.8 on Win 10. I am launching Matlab from an Anaconda prompt in the mne environment (https://mne.tools/stable/index.html).
I am calling a Python function using the standard syntax - ex: r = py.test.score(X,y).
The Issue:
I make changes to the Python code, save, and runfile which are immediately available in my Python ide (Spyder), but Matlab does run the latest version of the Python function. I have to restart Matlab - using the Anaconda prompt in the mne environment - each time I make a change to my Python code. Upon restart, the updated Python code is used by Matlab.
I update the current Python path prior to calling my Python function using:
if count(py.sys.path,'')==0
insert(py.sys.path,int32(0),'');
end
I have tested this with a basic "Hello World" type Python function and this is my experience.
I am not sure how to further troubleshoot this issue.
Is there something I may be doing incorrectly?
Thanks in advance,
Daniel
  2 件のコメント
Stijn Haenen
Stijn Haenen 2020 年 8 月 7 日
Is there an .asv file of the python script saved in your working folder. The .asv files are used as backup while matlab is running. Maybe you should change the .asv files of the python script.
Daniel Stolzberg
Daniel Stolzberg 2020 年 8 月 7 日
Thank you for your suggestion, Stijn.
I don't see any files with the .asv extension in my working folder.
There is a folder called __pycache__ which has a file called test.cpython-38.pyc in it. This seems to be a compiled python file.
Any other suggestions? It is quite inconvenient to restart Matlab each time I make a change to the Python script.

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

採用された回答

Sean de Wolski
Sean de Wolski 2020 年 8 月 7 日
Try this
clear classes
m = py.importlib.import_module('yourlib');
py.importlib.reload(m);
  2 件のコメント
Daniel Stolzberg
Daniel Stolzberg 2020 年 8 月 7 日
編集済み: Daniel Stolzberg 2020 年 8 月 7 日
Thanks for this solution, Sean!
Note to someone reading this in the future, clear classes will, of course, clear all workspace variables as well.
Thank you!
Ronan Fleming
Ronan Fleming 2021 年 4 月 12 日
https://nl.mathworks.com/help/releases/R2021a/matlab/matlab_external/out-of-process-execution-of-python-functionality.html

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

その他の回答 (3 件)

Chen
Chen 2021 年 5 月 14 日
Sean's answer will not work without wrapping a function around it.
Apparently, it's a known bug
function reloadPy()
warning('off','MATLAB:ClassInstanceExists')
clear classes
mod = py.importlib.import_module('mat');
py.importlib.reload(mod);
end
  2 件のコメント
Sean de Wolski
Sean de Wolski 2021 年 5 月 17 日
I might suggest modifying the warning part to this to restore state after.
s = warning('off','MATLAB:ClassInstanceExists');
restorer = onCleanup(@()warning(s));
Akshay Jajoo
Akshay Jajoo 2021 年 9 月 8 日
I am getting the following error: Unable to resolve the name py.importlib.reload.

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


MONIKA
MONIKA 2024 年 1 月 3 日
function reloadPy() warning('off','MATLAB:ClassInstanceExists') clear classes mod = py.importlib.import_module('mat'); py.importlib.reload(mod);

MONIKA
MONIKA 2024 年 1 月 3 日
function reloadPy() warning('off','MATLAB:ClassInstanceExists') clear classes mod = py.importlib.import_module('mat'); py.importlib.reload(mod);

カテゴリ

Help Center および File ExchangeCall Python from MATLAB についてさらに検索

タグ

製品


リリース

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by