473,772 Members | 3,665 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 28239
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\Python 25\lib\socket.p yc'>

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.setdefau lttimeout() 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
1888
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 environment variables which select the desired behaviour, and the module inspects those variables to determine the mode in which it should set itself up. I would prefer a pure Python solution, rather than one which depends on external state. Can you...
9
2866
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 that imports foo.py and now I want to use, say, os.walk in bar.py. Which is faster or more correct or whatever: Do I import os at the top of bar.py and use foo's functions?
10
5497
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 advance
4
2101
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 has no attribute 'actions'
1
7218
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 the desktop icons all disappearing and programs reloading, sometimes `Dr Watson Postmortum` error box arrives and the computer crashes - freezes Can anyone possibly help me cure this problem
3
2147
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 = GraphWin("Python Paint",600,600) win.setCoords(-100.0,-100.0,100.0,100.0)
40
3490
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: AttributeError: 'module' object has no attribute 'handlers' The only experience I have in modules is os and os.path ... if I do the same thing, simply import os and then type dir(os.path), it displays the contents as expected.
3
1462
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
1245
by: bvdp | last post by:
Terry Reedy wrote: <snip> <snip> <snip>
0
9454
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10261
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10104
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9912
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6715
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5354
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5482
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3609
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2850
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.