How to get current module object

Alex noname9968 at gmail.com
Mon Feb 18 11:49:02 EST 2008


Gabriel Genellina wrote:
> En Sun, 17 Feb 2008 16:25:44 -0200, Alex <noname9968 at gmail.com> escribi�:
>   
>> Can I get reference to module object of current module (from which the
>> code is currently executed)? I know __import__('filename') should
>> probably do that, but the call contains redundant information (filename,
>> which needs to be updated), and it'll perform unnecessary search in
>> loaded modules list.
>>
>> It shouldn't be a real problem (filename can probably be extracted from
>> the traceback anyway), but I wonder if there is more direct and less
>> verbose way.
>>     
> sys.modules[__name__]
>   
That's what I've been searching for, thanks. By the way, I know it might 
be trivial question... but function and class namespaces have __name__ 
attribute too. Why is global one always returned?
> Why do you want to get the module object? globals() returns the module  
> namespace, its __dict__, perhaps its only useful attribute...
To pass it as a parameter to a function (in another module), so it can 
work with several modules ("plugins" for main program) in a similar manner.



More information about the Python-list mailing list