Unimport module

Terry Reedy tjreedy at udel.edu
Sun Mar 13 23:18:23 EST 2005


"Chmouel Boudjnah" <chmouel at chmouel.com> wrote in message 
news:d130nh$88q$1 at sea.gmane.org...
> dir = listdir('/path/')
> for i in dir:
> sys.path.append('/path/' + i "/")
> import Module
> doStuff()
> sys.path.remove('/path/' + i "/")
>
> it's obviously not working since it get only the first import Module 
> while the others doent get replaced, the only solution for me was too :
>
> sys.modules.pop('Module')

Yes, Python's import mechanism assumes that sys.path is relatively static, 
or grow only.  I not sure what 'reload Module' would do after the first 
loop.

> which after it does works fine. But my question is what's happenning in 
> the internals, does it free the memory of Module to do a sys.modules.pop 
> ?

A Python implementation *may* 'free' an object anytime after the last 
reference to the object disappears.  Whether the name Module and the slot 
in sys.modules are the only two references depends, I believe, on both the 
code in the module and the code the uses it.  sys.getrefcount() can help 
answer this.

Terry J. Reedy






More information about the Python-list mailing list