python reload() function matlab 2014b

10 vues (au cours des 30 derniers jours)
Chris Barnhart
Chris Barnhart le 6 Jan 2015
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(????)

Réponse acceptée

uri merhav
uri merhav le 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 commentaire
Robert Snoeberger
Robert Snoeberger le 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].

Connectez-vous pour commenter.

Plus de réponses (3)

Raghav Paul
Raghav Paul le 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 le 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 le 23 Fév 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 commentaires
Justin Mai
Justin Mai le 5 Déc 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 le 7 Avr 2021
Modifié(e) : Blake Lundstrom le 7 Avr 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).

Connectez-vous pour commenter.

Catégories

En savoir plus sur Call Python from MATLAB dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by