开发者

Py2Exe Location [duplicate]

This question already has answers here: Closed 11 years ago.

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\Deskto开发者_如何学运维p\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.


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.


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.


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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜