Skip to content

Instantly share code, notes, and snippets.

@sherbang
Last active August 29, 2015 14:21
Show Gist options
  • Save sherbang/13007d7f9460e7181b99 to your computer and use it in GitHub Desktop.
Save sherbang/13007d7f9460e7181b99 to your computer and use it in GitHub Desktop.
Find program directory for running python program
import os
import sys
def get_app_directory():
"""Return the directory that contains the application.
This is the directory of the .py file in most cases, but is the directory
of the .exe file in the case of py2exe.
"""
import imp
if (hasattr(sys, "frozen") # new py2exe
or hasattr(sys, "importers") # old py2exe
or imp.is_frozen("__main__")): # tools/freeze
return os.path.abspath(os.path.dirname(sys.executable))
else:
import __main__
if hasattr(__main__, '__file__'):
return os.path.abspath(os.path.dirname(__main__.__file__))
else:
return os.path.abspath(os.path.dirname(sys.argv[0]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment