See also WhereAmI for another take on this problem.

ThomasHeller posted this tip to the mailing list:

main_is_frozen() returns True when running the exe, and False when running from a script.

get_main_dir() returns the directory name of the script or the directory name of the exe - this is also sometimes useful.

   1 import imp, os, sys
   3 def main_is_frozen():
   4    return (hasattr(sys, "frozen") or # new py2exe
   5            hasattr(sys, "importers") # old py2exe
   6            or imp.is_frozen("__main__")) # tools/freeze
   8 def get_main_dir():
   9    if main_is_frozen():
  10        return os.path.dirname(sys.executable)
  11    return os.path.dirname(sys.argv[0])

HowToDetermineIfRunningFromExe (last edited 2009-08-14 20:41:48 by ciphergoth)