Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to Get the Path of the executing frozen script
    primarykey
    data
    text
    <p>If you are running a frozen python script (frozen using py2exe) from a directory and drive different from where the script is present, what is the best way to determine the path of the executing script?</p> <p>Few solutions I have tried</p> <pre><code>inspect.getfile(inspect.currentframe()) </code></pre> <p><strong>Problem: Does not return the full path. It only returns the script name.</strong></p> <pre><code>os.path.abspath( __file__ ) </code></pre> <p><strong>Problem: Doesn't work on Windows</strong></p> <pre><code>os.path.dirname(sys.argv[0]) </code></pre> <p><strong>Problem: Returns empty string.</strong></p> <pre><code>os.path.abspath(inspect.getsourcefile(way3)) </code></pre> <p><strong>Will not work if the drive is different from the pwd</strong></p> <pre><code>os.path.dirname(os.path.realpath(sys.argv[0])) </code></pre> <p><strong>Will not work if the drive is different from the pwd</strong></p> <p>Here is a minimal not-working example</p> <pre><code>D:\&gt;path PATH=c:\Python27\;c:\Users\abhibhat\Desktop\ToBeRemoved\spam\dist\;c:\gnuwin32\bin D:\&gt;cat c:\Users\abhibhat\Desktop\ToBeRemoved\spam\eggs.py import os, inspect, sys def way1(): return os.path.dirname(sys.argv[0]) def way2(): return inspect.getfile(inspect.currentframe()) def way3(): return os.path.dirname(os.path.realpath(sys.argv[0])) def way4(): try: return os.path.abspath( __file__ ) except NameError: return "Not Found" def way5(): return os.path.abspath(inspect.getsourcefile(way3)) if __name__ == '__main__': print "Path to this script is",way1() print "Path to this script is",way2() print "Path to this script is",way3() print "Path to this script is",way4() print "Path to this script is",way5() D:\&gt;eggs Path to this script is Path to this script is eggs.py Path to this script is D:\ Path to this script is Not Found </code></pre> <p><strong>Related Questions:</strong></p> <ul> <li><a href="https://stackoverflow.com/questions/2259503/how-to-know-the-path-of-the-running-script-in-python">How to know the path of the running script in Python?</a></li> <li><a href="https://stackoverflow.com/questions/50499/in-python-how-do-i-get-the-path-and-name-of-the-file-that-is-currently-executin">How do I get the path and name of the file that is currently executing?</a></li> <li><a href="https://stackoverflow.com/questions/595305/python-path-of-script">python, path of script [closed]</a></li> </ul> <p><strong>Note</strong></p> <p>@Fenikso's solution will work if the script resides on the same drive where you are executing but when its on a different drive, it will not work</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload