python reload() function matlab 2014b

14 visualizaciones (últimos 30 días)
Chris Barnhart
Chris Barnhart el 6 de En. de 2015
Editada: Blake Lundstrom el 7 de Abr. de 2021
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(????)

Respuesta aceptada

uri merhav
uri merhav el 6 de En. de 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 comentario
Robert Snoeberger
Robert Snoeberger el 6 de Oct. de 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].

Iniciar sesión para comentar.

Más respuestas (3)

Raghav Paul
Raghav Paul el 4 de Sept. de 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 el 20 de En. de 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 el 23 de Feb. de 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 comentarios
Justin Mai
Justin Mai el 5 de Dic. de 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 el 7 de Abr. de 2021
Editada: Blake Lundstrom el 7 de Abr. de 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).

Iniciar sesión para comentar.

Categorías

Más información sobre Call Python from MATLAB en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by