468,549 Members | 1,974 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,549 developers. It's quick & easy.

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

Is it possible to unload a module after I've imported it.
I have the main program from which I import several smaller programs.
Some of these uses timeoutsocket module.
But one of the smaller program can not work with the timeoutsocket
module, so I must
unload the timeoutsocket module, execute the program and then reload
timeoutsocket module.
So, s it possible to unload a module after I've imported it..
Thanks
L.

Sep 29 '07 #1
3 26056
Have you tried a del?
>import socket
dir()
['__builtins__', '__doc__', '__name__', 'socket']
>del socket
dir()
['__builtins__', '__doc__', '__name__']

See you!

Sep 29 '07 #2
En Sat, 29 Sep 2007 17:32:15 -0300, marvinla <ma************@gmail.com>
escribi�:
Have you tried a del?
>>import socket
dir()
['__builtins__', '__doc__', '__name__', 'socket']
>>del socket
dir()
['__builtins__', '__doc__', '__name__']
pyimport socket
pydel socket
pyimport sys
pysys.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

Sep 29 '07 #3
On Sep 29, 9:32 pm, marvinla <marvinware2...@gmail.comwrote:
Have you tried a del?
import socket
dir()

['__builtins__', '__doc__', '__name__', 'socket']>del socket
dir()

['__builtins__', '__doc__', '__name__']

See you!
>>import socket
import sys
'socket' in sys.modules
True
>>del sys.modules['socket']
'socket' in sys.modules
False

Although as Gabriel says, this may not be the best approach for this
particular need.

A new import will do a full reload of the socket module.

Michael
http://www.manning.com/foord

Sep 30 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

17 posts views Thread by Jacek Generowicz | last post: by
9 posts views Thread by BartlebyScrivener | last post: by
4 posts views Thread by noagbodjivictor | last post: by
40 posts views Thread by rjcarr | last post: by
3 posts views Thread by Jugdish | last post: by
4 posts views Thread by bvdp | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.