PDA

View Full Version : Tips and tricks : Totally unload a python library


solexalex
2005-07-22, 00:34
hi !
did you already noticed that when you 'import' a library in a script, if you modify this library then launch again the script, your change does not take effect ??
even if you do a
del libraryname
at the end, you'll always get this library loaded...

to prevent this, i found this trick (by myself... so maybe is there some better ideas !!! i must say that i don't really know)

try:
del sys.modules['mymodule']
except:
pass

maybe is it possible the whole sys.modules dictionnary, but i'm really not sure and i really think it is a bad idea !!

just tell me what you think about this trick !
cya

EnderW
2005-07-22, 00:45
wonderful, that's actually very useful for me. thanks a lot!

have yet to try it though...

solexalex
2005-07-22, 01:18
i hope this is really a good idea and that it works for all !!

i can't imagine the number of coders who got a headacke because of changes in their scripts that never happens if xbmc is not restarted...

in my opinion, this is a bug of xbmc because py modules are optimized and saved as .pyo files. .pyo files should be their date compared to the .py file date and if different, should be re-optimized... (not sure to be very easy to understand...)

Nuka1195
2005-07-22, 01:36
hi,

i use:


try:
reload(mymodule)
except:
import mymodule


not sure which is better.

darkie
2005-07-22, 04:56
in my opinion, this is a bug of xbmc because py modules are optimized and saved as .pyo files. .pyo files should be their date compared to the .py file date and if different, should be re-optimized... (not sure to be very easy to understand...)
are you sure its a bug in xbmc? looks to me that it is pythons fault *:d.
the real problem however is that python is never unloaded once it is started (by running a script). this means that all libraries that are loaded while xbmc is running are never unloaded. python will just reuse the loaded libraries and won't even bother to look for a new one until python is completly shut down. this is the way how python works and cannot be changed.

solexalex
2005-07-22, 15:11
hello !
as i said, i can't be sure of what i say... these are only suppositions as i'm not a python guru and not a natively english speaker...
i supposed that reading this :
http://www.python.org/doc....0000000 (http://www.python.org/doc/2.0.1/tut/node8.html#section008120000000000000000)
when the python interpreter is invoked with the -o flag, optimized code is generated and stored in .pyo files. the optimizer currently doesn't help much; it only removes assert statements and set_lineno instructions. when -o is used, all bytecode is optimized; .pyc files are ignored and .py files are compiled to optimized bytecode.and i thought optimization = keep a trace in memory... i thought it was very linked