[Wolves] python module query

Stuart Langridge sil at kryogenix.org
Mon Sep 6 21:52:58 BST 2004


sparkes wrote:
> Stuart Langridge wrote:
> 
>> sparkes wrote:
>>
>>> exec 'import ' + <variable> + ' as my_mod'
>>
>>
>>
>> Eek. Don't do that.
>>
>> foo = __import__('foo')
> 
> 
> you mean
> 
> foo = __import__(foo)
> 
> ;-)

I most certainly do not. The point is that you pass a string to 
__import__. :)

so:

modulename = 'sys'
sys = __import__(modulename)

is the same as

import sys

> incidentally I am using reload() to unload the module (this method is 
> later called again to load, run and unload other modules (except they 
> aren't really modules but python scripts in the same directory)), can 
> this really be right?  It works but it doesn't look as readable as the 
> rest of the code do to the fact reload certainly doesn't mean unload in 
> english ;-)

Why do you want to unload a module? Plus, what are you expecting 
"unloading" it to do? If you want to stop code getting at it, simply 
rebind its name:

import sys
sys = None

reload() reloads (wouldja believe it?) a module from disc, if it's 
changed on disc since it was imported. I've never once used it in a 
program, but I use it a lot when testing a new module from the Python 
interpreter. ("import module", test it, damn it doesn't work, 
"reload(module)", test again, go to step 3).

Aq.



More information about the Wolves mailing list