How do you show the path of the current running python module?
Which would print /Users/user/documents/python/bla.py
if bla.py was placed in /Users/user/documents/python/
.
Asked
Active
Viewed 9,964 times
11

systempuntoout
- 71,966
- 47
- 171
- 241
2 Answers
14
If you want a module to know where it's at even when imported, you should be able to use
import sys
sys.modules[__name__].__file__
I know this includes the absolute path on my linux system but I've heard it said that it doesn't on Windows.

aaronasterling
- 68,820
- 20
- 127
- 125
-
It doesn't include absolute path even on linux system. Tested against Debian linux 6 – Tyler Liu Nov 17 '11 at 11:43
-
@Tyler weird. I was running ubuntu 10.04 at the time I posted this answer. Thanks for the heads up. – aaronasterling Nov 17 '11 at 21:06
8
import os
print os.path.abspath(__file__)

systempuntoout
- 71,966
- 47
- 171
- 241
-
2It should be noted that this only works for files in the same directory as the file that was invoked to start execution of the program. – aaronasterling Nov 26 '10 at 22:32
-
@aaron you are right, I think it should fit what OP is asking for – systempuntoout Nov 26 '10 at 22:33