python reload() function matlab 2014b

14 views (last 30 days)
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(????)

Accepted Answer

uri merhav
uri merhav on 6 Jan 2015
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 Comment
Robert Snoeberger
Robert Snoeberger on 6 Oct 2015
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].

Sign in to comment.

More Answers (3)

Raghav Paul
Raghav Paul on 4 Sep 2015
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 on 20 Jan 2015
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 on 23 Feb 2015
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 Comments
Justin Mai
Justin Mai on 5 Dec 2019
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 on 7 Apr 2021
Edited: Blake Lundstrom on 7 Apr 2021
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).

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!