2011-06-13 41 views

回答

2

py2exe将sys.executable设置为可执行文件的完整路径名。 sys.argv[0]也应该给这条路。官方文档见Py2exeEnvironment

1

您的.exe文件的路径应存储在sys.executable中。

该py2exe.org维基WhereAmI页面有一些有用的例子,你也可以找到有用的。

有趣的是,the top-voted answera similar question from last year拥护者使用sys.argv[0]而不是sys.executable。任何一个应该可以在py2exe中正常工作。如果sys.frozen已设置,我一直使用sys.executable,并且从来没有任何问题,但是使用sys.argv[0]也不会有任何伤害。

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