How to find the package a module belongs to ?

Gordon McMillan gmcm at hypernet.com
Fri Jun 1 17:02:40 EDT 2001


Jeff Sandys wrote:
...
[Sasa Zivkov]
>> I have a package containing several modules.
>> Is is possible, within a module, 
>> to say "give me the package I belong to" ?

Strictly speaking, no, not automatically, because module x within package p 
may be loaded as p.x or just as x.

[Jeff]
>It would sure be nice to pull this information from some 
>secret variable without having to explicitly name them.    

But every module has a __file__ attribute that will tell you (whether the 
module is loaded as x or p.x) that it came from some/path/p/x.py.

- Gordon



More information about the Python-list mailing list