0

Possible Duplicate:
how can i get the executable's current directory in py2exe?

Is there a way to get the path to the executable file currently running with py2exe? For example if I had an executable generated with py2exe stored in C:\Users\Desktop\John\myapp.exe, how can I get this path from within myapp.exe in Python in Wondows?

Thank you for any help in advance - I really appreciate it.

Community
  • 1
  • 1

3 Answers3

2

py2exe sets sys.executable to the full pathname of the executable. sys.argv[0] should also give this path. See Py2exeEnvironment for the official documentation.

interjay
  • 107,303
  • 21
  • 270
  • 254
1

The path to your .exe file should be stored in sys.executable.

The py2exe.org wiki WhereAmI page has some useful examples that you may find helpful as well.

Interestingly, the top-voted answer to a similar question from last year advocates using sys.argv[0] instead of sys.executable. Either one should work fine from within py2exe. I've always used sys.executable if sys.frozen was set, and never had any issues, but there shouldn't be any harm in using sys.argv[0] instead, either.

Community
  • 1
  • 1
Daniel Pryden
  • 59,486
  • 16
  • 97
  • 135
0

I use something like the following:

def get_app_info():
  # determine if application is a script file or frozen exe
  application = {'path': './', 'dir': './', 'start': None}

  if hasattr(sys, 'frozen'):
    application['path']   = sys.executable
    application['dir']    = os.path.dirname(sys.executable)
  else:
    application['path']   = os.path.abspath(sys.argv[:1][0])
    application['dir']    = sys.path[0]
  return application
nZeeR
  • 31
  • 2