21

我使用 PyQt4 在 Python 3.1 中构建了一个相当简单的应用程序。完成后,我希望将应用程序分发到没有安装任何一个的计算机上。

我几乎只关心 Windows 平台,所以我的目标是最终拥有一个可执行文件以及一些资源文件和 .dll。

搜索了一圈,我得出的结论是

  • py2exe仅支持 Python 至 2.7 版本
  • pyinstaller仅支持 Python 至 2.6 版本
  • cx_Freeze对我不起作用,因为我在尝试执行成功构建的二进制文件时不断收到以下错误:

Y:\Users\lulz\build\exe.win32-3.1>system_shutdown.exe
Traceback (most recent call last):
File "Y:\Program Files (x86)\Python\lib\site-packages\cx_Freeze\initscripts\Console3.py", line 27, in exec(code, m.__dict__)
File "Y:/Users/lulz/Documents/Coding/Python3/projects/System Shutdown/system_shutdown.pyw", line 5, in from PyQt4 import QtCore
File "ExtensionLoader_PyQt4_QtCore.py", line 16, in AttributeError: 'NoneType' object has no attribute 'modules'

所以我的问题基本上是这里的两个问题:

  1. 除了 cx_Freeze 之外,还有其他方法可以使用我的配置构建二进制文件吗?
  2. 如果不是,cx_Freeze 问题可能是什么?

如有必要,我可以提供有关第二个问题的更多信息,例如我对 cx_Freeze 的调用、我的 distutils 设置脚本等。

已经感谢您的帮助和评论。

4

2 回答 2

13

您可以通过将一行代码附加到 cx_Freeze 包中的 freeze.py 来解决此问题。

它在这里描述:http: //www.mail-archive.com/cx-freeze-users@lists.sourceforge.net/msg00212.html

它至少对我有用:)

干杯,阿尔玛

于 2009-11-19T20:18:38.830 回答
-1

对于 Python 3.3 及更高版本,这里有一个很好的解决方案: py2exe - generate single executable file

安装py2exe:

pip install py2exe

然后除了 'your_script.py' 文件之外,添加以下 'Make_exe.py' 文件:

from distutils.core import setup
import py2exe, sys

class Make_exe():
    def __init__(self, python_script):
        sys.argv.append('py2exe')

        setup(
            console=[{'script': python_script}],
            zipfile = None,
            options={
                'py2exe': 
                {
                    'bundle_files': 1, 
                    'compressed': True,
                    # Add includes if necessary, e.g. 
                    'includes': ['lxml.etree', 'lxml._elementpath', 'gzip'],
                }
            }
        )

if __name__ == '__main__':
    Make_exe('your_script.py')

如果你想让'your_script.py'每次在 python 中运行时都将其重新构建为 'your_script.exe' ,你可以添加到它的 main 中:

import subprocess
import sys

if __name__ == '__main__':
    currentFile = sys.argv[0]
    if currentFile.lower().endswith(".py"):
        exitCode = subprocess.call("python Make_exe.py")
        if exitCode==0 :
            dirName = os.path.dirname(currentFile)
            exeName = os.path.splitext(os.path.basename(currentFile))[0] + '.exe'
            exePath = dirName + "/dist/" + exeName
            cmd = [exePath] + sys.argv[1:]
            print ("Executing command:\n %s" % cmd)
            exitCode = subprocess.call(cmd)
        sys.exit(exitCode)
    else:
        print ("This will be executed only within the new generated EXE File...")
于 2016-01-22T00:51:12.540 回答