可能重复:
how can i get the executable's current directory in py2exe?Py2Exe位置
有没有办法让路径可执行文件与当前运行py2exe?例如,如果我有一个用C:\ Users \ Desktop \ John \ myapp.exe存储的py2exe生成的可执行文件,我如何从Wondows中的myapp.exe中得到此路径?
感谢您提前提供任何帮助 - 我真的很感激。
可能重复:
how can i get the executable's current directory in py2exe?Py2Exe位置
有没有办法让路径可执行文件与当前运行py2exe?例如,如果我有一个用C:\ Users \ Desktop \ John \ myapp.exe存储的py2exe生成的可执行文件,我如何从Wondows中的myapp.exe中得到此路径?
感谢您提前提供任何帮助 - 我真的很感激。
py2exe将sys.executable
设置为可执行文件的完整路径名。 sys.argv[0]
也应该给这条路。官方文档见Py2exeEnvironment。
您的.exe
文件的路径应存储在sys.executable
中。
该py2exe.org维基WhereAmI页面有一些有用的例子,你也可以找到有用的。
有趣的是,the top-voted answer到a similar question from last year拥护者使用sys.argv[0]
而不是sys.executable
。任何一个应该可以在py2exe中正常工作。如果sys.frozen
已设置,我一直使用sys.executable
,并且从来没有任何问题,但是使用sys.argv[0]
也不会有任何伤害。
我使用类似以下内容:
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