Problem with Dynamically unloading a module
Steven D'Aprano
steve at REMOVE-THIS-cybersource.com.au
Wed Dec 23 08:40:49 EST 2009
On Wed, 23 Dec 2009 13:37:06 +0100, Jean-Michel Pichavant wrote:
> 3/ if you really need to unload the previous module, it's a little bit
> tedious.
>
> import mod1
> del mod1
> sys.modules['mod1'] = None
Assigning sys.modules[name] to None is not the same as deleting the
entry. None has special meaning to imports from packages, and for modules
it is interpreted as meaning that the module doesn't exist.
>>> import math
>>> del math
>>> sys.modules['math'] = None
>>> import math
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named math
> # will unload mod1 assuming mod1 was the only
> reference to that module.
Which is highly unlikely. Any classes or functions from the module will
keep the module alive.
> But believe me, you don't want to mess up with the python import
> mechanism.
Unless you understand how it works.
--
Steven
More information about the Python-list
mailing list