sys.argv[0] doesn't always contain the full path of running script.

Joel Hedlund joel.hedlund at gmail.com
Thu Aug 31 11:47:33 EDT 2006


 >> How can I find where exactly the current python script is running?
 >
> Doesnt __file__ attribute of each module contain the full filepath of
> the module?
 >

Yes indeed! But the path to the module will not be the same as the path to 
the script if you are currently in an imported module. Consider this:

my_script.py:
---------------------------
import my_module

---------------------------

my_module.py:
---------------------------
print __file__

---------------------------

Running "python test.py" now prints /path/to/my_module.py, not 
/path/to/my_script.py.

Cheers!
/Joel Hedlund



More information about the Python-list mailing list