howto load and unload a module
Peter Hansen
peter at engcorp.com
Fri Jun 24 09:01:52 EDT 2005
Guy Robinson wrote:
> Some of these scripts could potentially be quite large. Also the list of
> scripts could be quite large. So the main reason for unloading modules
> is to save memory.
Unless you're talking megabytes of bytecode (do a "wc *.pyc" on the
compiled files and see) it's probably not worth the bother.
Still, assuming the modules are pure Python, and don't do strange things
like inject references to themselves or their data into other places
(i.e. other modules, including sys or builtins), it should be possible
to unload them simply by deleting all references to them, *including*
manually removing them from sys.modules.
How do you plan to import them? Using the import statement, or
__import__, or some other means? How you do it will determine exactly
what steps are required to free them up.
Note also that this won't necessarily release any memory back to the
operating system, and it won't necessarily unload any extension modules
or other shared libraries that are loaded. The whole concept of
"unloading" a module is pretty much undefined in Python, so whatever you
can get is the best you can expect...
-Peter
More information about the Python-list
mailing list