There are a lot of methods to get the path of a script in Python, but the following method always work, even you use py2exe or PyInstaller to compile the script to binary executable file.
1 2 3 4 5 6 import sys, os print 'sys.argv =', sys.argv pathname = os.path.dirname(sys.argv) print 'path =', pathname print 'full path =', os.path.abspath(pathname)
Note there is a big difference between the path of the script and the current path, ie the “.” path in Windows and POSIX systems. When you run the script from other folder, such like this:
1 $python bin/myscript.py
Then the current folder is /home/leon/, while the path of the script is /home/leon/bin.