473,221 Members | 2,334 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,221 software developers and data experts.

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 28160
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

17
by: Jacek Generowicz | last post by:
I have a module whose behaviour needs to be configurable. The module needs to decide, the first time it is imported, beteween alternative interfaces it presents. Currently, I set some...
9
by: BartlebyScrivener | last post by:
I know this must have been answered a hundred times, but I must be searching on the wrong terminology. Let's say I have a module foo.py that imports os. I make another script called bar.py...
10
by: Bonzol | last post by:
vb.net Hey there, could someone just tell me what the differnce is between classes and modules and when each one would be used compared to the other? Any help would be great Thanx in...
4
by: noagbodjivictor | last post by:
I have a variable names actions in a module named qt_actions.py Well this is what I get: Traceback (most recent call last): File "<stdin>", line 1, in <module> AttributeError: 'module' object...
1
by: steveking4557 | last post by:
Hi I keep getting the following error message Faulting application explorer.exe, version 6.0.2900.2180, faulting module unknown, version 0.0.0.0, fault address 0x0240640e Which results in...
3
by: GIJosh | last post by:
Hi guys. I'm new to python, so go easy. Heres my problem. I've got my main file, Python Paint.py #Python Paint.py from graphics import * import time import functions win =...
40
by: rjcarr | last post by:
Sorry if this is a completely newbie question ... I was trying to get information about the logging.handlers module, so I imported logging, and tried dir(logging.handlers), but got: ...
3
by: Jugdish | last post by:
Why doesn't the following work? $HOME/pkg/__init__.py $HOME/pkg/subpkg/__init__.py $HOME/pkg/subpkg/a.py $HOME/pkg/subpkg/b.py # empty import a
4
by: bvdp | last post by:
Terry Reedy wrote: <snip> <snip> <snip>
0
by: veera ravala | last post by:
ServiceNow is a powerful cloud-based platform that offers a wide range of services to help organizations manage their workflows, operations, and IT services more efficiently. At its core, ServiceNow...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
by: jimatqsi | last post by:
The boss wants the word "CONFIDENTIAL" overlaying certain reports. He wants it large, slanted across the page, on every page, very light gray, outlined letters, not block letters. I thought Word Art...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.