Find path to currently running file

Python

Python Problem Overview


How can I find the full path to the currently running Python script? That is to say, what do I have to do to achieve this:

$ pwd
/tmp
$ python baz.py
running from /tmp 
file is baz.py

Python Solutions


Solution 1 - Python

__file__ is NOT what you are looking for. Don't use accidental side-effects

sys.argv[0] is always the path to the script (if in fact a script has been invoked) -- see http://docs.python.org/library/sys.html#sys.argv

__file__ is the path of the currently executing file (script or module). This is accidentally the same as the script if it is accessed from the script! If you want to put useful things like locating resource files relative to the script location into a library, then you must use sys.argv[0].

Example:

C:\junk\so>type \junk\so\scriptpath\script1.py
import sys, os
print "script: sys.argv[0] is", repr(sys.argv[0])
print "script: __file__ is", repr(__file__)
print "script: cwd is", repr(os.getcwd())
import whereutils
whereutils.show_where()

C:\junk\so>type \python26\lib\site-packages\whereutils.py
import sys, os
def show_where():
    print "show_where: sys.argv[0] is", repr(sys.argv[0])
    print "show_where: __file__ is", repr(__file__)
    print "show_where: cwd is", repr(os.getcwd())

C:\junk\so>\python26\python scriptpath\script1.py
script: sys.argv[0] is 'scriptpath\\script1.py'
script: __file__ is 'scriptpath\\script1.py'
script: cwd is 'C:\\junk\\so'
show_where: sys.argv[0] is 'scriptpath\\script1.py'
show_where: __file__ is 'C:\\python26\\lib\\site-packages\\whereutils.pyc'
show_where: cwd is 'C:\\junk\\so'

Solution 2 - Python

This will print the directory in which the script lives (as opposed to the working directory):

import os
dirname, filename = os.path.split(os.path.abspath(__file__))
print "running from", dirname
print "file is", filename

Here's how it behaves, when I put it in c:\src:

> cd c:\src
> python so-where.py
running from C:\src
file is so-where.py

> cd c:\
> python src\so-where.py
running from C:\src
file is so-where.py

Solution 3 - Python

import sys, os

file = sys.argv[0]
pathname = os.path.dirname(file)
print 'running from %s' % os.path.abspath(pathname)
print 'file is %s' % file

Check os.getcwd() (docs)

Solution 4 - Python

The running file is always __file__.

Here's a demo script, named identify.py

print __file__

Here's the results

MacBook-5:Projects slott$ python StackOverflow/identify.py 
StackOverflow/identify.py
MacBook-5:Projects slott$ cd StackOverflow/
MacBook-5:StackOverflow slott$ python identify.py 
identify.py

Solution 5 - Python

The script name will (always?) be the first index of sys.argv:

import sys
print sys.argv[0]

An even easier way to find the path of your running script:

os.path.dirname(sys.argv[0])

Solution 6 - Python

I would suggest

import os, sys
print os.path.split(os.path.abspath(os.path.realpath(sys.argv[0])))[0]

This way you can safely create symbolic links to the script executable and it will still find the correct directory.

Solution 7 - Python

The directory of the script which python is executing is added to sys.path This is actually an array (list) which contains other paths. The first element contains the full path where the script is located (for windows).

Therefore, for windows, one can use:

import sys
path = sys.path[0]
print(path)

Others have suggested using sys.argv[0] which works in a very similar way and is complete.

import sys
path = os.path.dirname(sys.argv[0])
print(path)

Note that sys.argv[0] contains the full working directory (path) + filename whereas sys.path[0] is the current working directory without the filename.

I have tested sys.path[0] on windows and it works. I have not tested on other operating systems outside of windows, so somebody may wish to comment on this.

Solution 8 - Python

Aside from the aforementioned sys.argv[0], it is also possible to use the __main__:

import __main__
print(__main__.__file__)

Beware, however, this is only useful in very rare circumstances; and it always creates an import loop, meaning that the __main__ will not be fully executed at that moment.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionChris BunchView Question on Stackoverflow
Solution 1 - PythonJohn MachinView Answer on Stackoverflow
Solution 2 - PythonRichieHindleView Answer on Stackoverflow
Solution 3 - PythonNathanView Answer on Stackoverflow
Solution 4 - PythonS.LottView Answer on Stackoverflow
Solution 5 - PythonWhatIsHeDoingView Answer on Stackoverflow
Solution 6 - Pythonuser118867View Answer on Stackoverflow
Solution 7 - PythonD.LView Answer on Stackoverflow
Solution 8 - PythonHoverHellView Answer on Stackoverflow