473,396 Members | 1,608 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,396 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 28196
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.