[Wolves] python module query

sparkes sparkes at westmids.biz
Tue Sep 7 07:31:57 BST 2004


Stuart Langridge wrote:

> 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__. :)

But foo contains the string with the name of the module

>
> so:
>
> modulename = 'sys'
> sys = __import__(modulename)
>
> is the same as
>
> import sys


yup that's what I said isn't it ;-)

>
>> 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

ok cool, that makes more sense.


cheers mate

sparkes



More information about the Wolves mailing list