Self-awareness of imported modules? Do they know where they live?

Heiko Wundram modelnine at bit-bukket.org
Thu Dec 15 02:52:02 EST 2005


Martin M. wrote:
> So how can I do this? In AppleScript I would try something like "path
> to me", but this does not work for imported scripts in AppleScript, as
> you then only get the path to the main script. But can this be done in
> Python? Can modules know where they are currently located, where they
> live? Can they be self-aware?

Yes, they can be:

modelnine at phoenix ~ $ cat test.py
import os

print os.path.abspath(__file__)
modelnine at phoenix ~ $ python test.py
/home/modelnine/test.py
modelnine at phoenix ~ $

HTH!

--- Heiko.



More information about the Python-list mailing list