How to unload a module after I've imported it.

Gabriel Genellina gagsl-py2 at yahoo.com.ar
Sat Sep 29 23:16:08 CEST 2007


En Sat, 29 Sep 2007 17:32:15 -0300, marvinla <marvinware2005 at gmail.com>  
escribi�:

> Have you tried a del?
>
>>> import socket
>>> dir()
> ['__builtins__', '__doc__', '__name__', 'socket']
>>> del socket
>>> dir()
> ['__builtins__', '__doc__', '__name__']

py> import socket
py> del socket
py> import sys
py> sys.modules['socket']
<module 'socket' from 'c:\apps\Python25\lib\socket.pyc'>

del only removes the reference from the current namespace, but the module  
is still loaded and available.
del sys.modules['socket'] would remove the module so the next import  
statement will have to reload it.

Back to the original question, timeoutsocket replaces some objects in the  
socket module with its own versions, just "unloading" timeoutsocket would  
not be enough, the changes had to be reverted.

timeoutsocket is an old hack for Python 2.2 and earlier. Since 2.3 you can  
achieve the same thing using socket.setdefaulttimeout() so unless you are  
forced to use such ancient versions, it can be dropped.

-- 
Gabriel Genellina




More information about the Python-list mailing list