【问题标题】:Building executables for Python 3 and PyQt为 Python 3 和 PyQt 构建可执行文件
【发布时间】:2010-12-16 18:43:40
【问题描述】:

我使用 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 设置脚本等。

感谢您的帮助和 cmets。

【问题讨论】:

  • 好问题。 py2exe 过去对我们的目的非常有用。
  • py2exe 现在可用于 Python 3!

标签: python-3.x pyqt4 py2exe pyinstaller


【解决方案1】:

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

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

至少对我有用 :)

干杯, 阿尔玛

【讨论】:

  • 我会尽快尝试并发布我的结果。到目前为止谢谢!
  • 这终于奏效了,必须添加您提到的代码行并包含 sip。现在剩下的问题是如何在启动我漂亮的 GUI 应用程序时抑制控制台窗口。
  • 有点晚了,但对于未来的读者,您可以通过使用 Win32GUI 基础冻结来抑制控制台窗口。 Example here.
  • 我通过pip安装了cx_freeze 4.3.3。我正在使用python 3.4。我应该在哪里尝试添加这行代码?我没有找到任何 freeze.py。不过,Scripts 中有一个 cxfreeze.py。
【解决方案2】:

对于 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'重建自己为'your_script.exe'每次你在python中运行它,你可以添加到它的主:

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...")

【讨论】:

    猜你喜欢
    • 2014-08-07
    • 1970-01-01
    • 2015-11-03
    • 1970-01-01
    • 1970-01-01
    • 2010-12-09
    • 2016-12-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多