python reload() function matlab 2014b

10 ビュー (過去 30 日間)
Chris Barnhart
Chris Barnhart 2015 年 1 月 6 日
編集済み: Blake Lundstrom 2021 年 4 月 7 日
I'm trying to use the python reload() method to reload my an edited python class that I've placed in "myfile.py"
After I first create the object and test it in matlab, changes to the .py file aren't seen. The python IDE has the same issue and one needs to call the built in method reload() - but its unclear what argument to provide it from matlab.
start with creating the object myobj in file myfile.py obj = py.myfile.myobj() then I edit "myfile.py" and need to reload it for the changes to be seen.
py.reload(????)

採用された回答

uri merhav
uri merhav 2015 年 1 月 6 日
From the matlab docs under features not supported:
Editing and reloading a Python® module in the same MATLAB session. To use an updated module, restart MATLAB.
A very weird limitation at that, I know. I tried working around it but to no avail...
  1 件のコメント
Robert Snoeberger
Robert Snoeberger 2015 年 10 月 6 日
The ability to reload edited Python code is available in R2015b. See the item "MATLAB Interface to Python: Clear Python class definitions with clear classes command, useful when reloading revised Python classes" under Advanced Software Development in the R2015b release notes [1].

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

その他の回答 (3 件)

Raghav Paul
Raghav Paul 2015 年 9 月 4 日
This functionality has been incorporated in R2015b using the MATLAB 'clear' command and Python 'import_module' and 'reload' commands. For an example, refer to the link below:

Chris Barnhart
Chris Barnhart 2015 年 1 月 20 日
Uri,
I started using system('matlab') to automatically start a new session as a reload() workaround. Seems as though python stdout appears in the first matlab session. Hopefully it'll help you.

Chuck37
Chuck37 2015 年 2 月 23 日
Not being able to reload edited python is a real drag. I hope they fix this soon. Restarting matlab takes too long to fit into a debug loop.
  5 件のコメント
Justin Mai
Justin Mai 2019 年 12 月 5 日
it still does not work. followed all steps:
>> clear classes
>> VectorFuncs = py.importlib.import_module('VectorImportFunctions');
>> py.importlib.reload(VectorFuncs);
>> filesVector = cell(py.VectorImportFunctions.get_vector_files_list());
which throws an error about syntax in my code -- which I know I fixed because I added something in upper lines which definitely should fail as a syntax error in python, but isn't reflected as failing in matlab.
The only thing that works is to restart MATLAB, unfortunately. This doesn't even happen all the time; sometimes the relaod process works (even though it's still incredibly silly of a process in my ignorant opinion).
Thoughts? Thanks.
Blake Lundstrom
Blake Lundstrom 2021 年 4 月 7 日
編集済み: Blake Lundstrom 2021 年 4 月 7 日
In R2020a (and likely everything after R2015b), the ability to reload python code does appear to work, but there is a caveat: the python code directly in the module being imported (VectorImportFunctions.py in Justin's example) DOES get reloaded, but any code that that module imports will not be reloaded until MATLAB is restarted (e.g., VectorImportFunctions.py has from X import Y in it, the code in X will not be reloaded until a restart). A work-around to this is to also reload the module being imported (so import and reload X in MATLAB in this example, even though it isn't strictly necessary to import it to MATLAB since the original python module imports it). This worked for me when using in-process python execution (the default).
For what it is worth, in the process of debugging this issue, I tried out-of-process execution (see https://www.mathworks.com/help/matlab/matlab_external/out-of-process-execution-of-python-functionality.html), since there were examples for reloading the process. However, MATLAB (R2020a, Win10) just crashes right before execution of my code every time I try to use the out-of-process option (even when running MATLAB as admin), so not recommended (beyond the fact that Mathworks indicates out-of-process adds overhead).

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

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by