views:

301

answers:

3

How can I get a reference to a module from within that module? Also, how can I get a reference to the package containing that module?

+10  A: 
import sys
current_module = sys.modules[__name__]
truppo
+2  A: 

If you have a class in that module, then the __module__ property of the class is the module of the class.

Michael
+3  A: 

You can get the name of the current module using __name__

The module reference can be found in the sys.modules dictionary.

See the Python documentation

pkit
__main__ is only the module name if it's run as a script: if you import the module this won't work.
mavnn
DOH! Typo, edited.
pkit