How can I clear specific Python classes/modules from memory without using "clear classes"
Mostrar comentarios más antiguos
I have successfully developed a workflow that will allow me to call custom python code from Matlab, see the following two scripts:
MATLAB:
function out = reloadPy() % A Matlab function
clear classes
mod = py.importlib.import_module('mymod');
py.importlib.reload(mod);
v1 = 1:5;
v2 = 6:10;
out = double(py.mymod.add2Vectors(v1,v2));
Python:
# mymod.py - a python function
import numpy
import array
def add2Vectors(v1, v2):
myarray = numpy.array(v1) + numpy.array(v2)
return array.array('d', myarray)
If I call reloadPy from the command line I get the following output:
>> test = reloadPy
Warning: Objects of 'onCleanup' class exist. Cannot clear this class or any of its superclasses.
> In reloadPy (line 2)
Warning: Objects of 'table' class exist. Cannot clear this class or any of its superclasses.
> In reloadPy (line 2)
Warning: Objects of 'datetime' class exist. Cannot clear this class or any of its superclasses.
> In reloadPy (line 2)
test =
7 9 11 13 15
>>
The warnings are stemming from the "clear classes" command. I am using that sequence of commands (lines 2-4) to reload the python module while debugging. Any changes I make to mymod.py are not captured unless I execute all 3 commands. The documentation for "clear" states:
Calling clear all, clear classes, and clear functions decreases code performance, and is usually unnecessary. ...
To clear a particular class, use clear myClass.
Is there a way to just clear just my python class or module? I can't find the name. I tried
[M, X, C] = inmem
But none of the result seem to be related to python.
1 comentario
Emil Geiger
el 9 de Jul. de 2018
Respuestas (2)
Emil Geiger
el 19 de Jul. de 2018
2 comentarios
Minchae Jeong
el 25 de Nov. de 2021
Editada: Minchae Jeong
el 25 de Nov. de 2021
Love you my brother this helped me a lot!
Arwel
el 5 de Mzo. de 2024
Thanks Emil! This also worked for me, and this makes it general for my use-case (via a file)....
function reloadPy(name)
% Save the module name (so we can reload it after clear..)
save('tempSave','name');
warning('off','MATLAB:ClassInstanceExists')
clear classes
% Reload the name....
name = load('tempSave');
name = name.name;
% Now reload the module....
mod = py.importlib.import_module(name);
py.importlib.reload(mod);
% Get rid of our temporary file...
delete('tempSave.mat');
Fan Yang
el 20 de Jun. de 2022
Say, if we want to reload 'util.py':
py.util = py.importlib.reload(py.importlib.import_module('util'));
This line would work for me. It actually redefined a struct with name 'py', so it may overwrite other module you imported, but if all of your python module is explicitly imported in this way, there seems to be no problem. Anyway this should be regarded as a workaround as well.
3 comentarios
Peter Qiang
el 29 de Mayo de 2023
This question has been bothering me for a long time. Many thanks for your solution!
Is a very useful method because in my 2021b with py 3.9, command line like:
clear classes
myModule = py.importlib.import_module('myModule');
py.importlib.reload(myModule);
just not working properly in my environment.
I guess cache still exist in somewhere after run that codes. So overwrite it seems become the only way works for me. To avoid overwrite matlab built-in py module I changed module variable name by remove perfix "py." , like:
myModule = py.importlib.reload(py.importlib.import_module('myModule'));
then call our module function with:
myModule.myFunction(args)
Thus it won't overwrite py. It works for me, at least, for now.
Lucademicus
el 8 de Dic. de 2023
It does not work for me in 2023B. Does anyone have another solution for me?
Arwel
el 5 de Mzo. de 2024
Ditto for me - no joy with 2023b....
Categorías
Más información sobre Python Package Integration en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!