使用 cx_freeze 将 .py 转换为可执行文件时出错

Posted

技术标签:

【中文标题】使用 cx_freeze 将 .py 转换为可执行文件时出错【英文标题】:Error converting .py to executable using cx_freeze 【发布时间】:2014-04-01 21:09:36 【问题描述】:

我正在使用 python 3.3.3

以下是我的 setup.py 代码

    import sys
from cx_Freeze import setup, Executable


build_exe_options = "packages": ["os"], "excludes": ["tkinter"]

base = None
if sys.platform == "win32":
    base = "Win32GUI"

setup(  name = "send_email",
        version = "0.1",
        description = "send the email",
        options = "build_exe": build_exe_options,
        executables = [Executable("send_email.py", icon="icon.ico", base=base)])  

我的 send_email.py 文件中唯一的导入是 smtplib。

以下错误消息是我在命令窗口中构建可执行文件时收到的消息:

c:\Python33>python.exe setup.py build
running build
running build_exe
copying c:\Python33\lib\site-packages\cx_Freeze\bases\Win32GUI.exe -> build\exe.
win-amd64-3.3\send_email.exe
copying C:\Windows\SYSTEM32\python33.dll -> build\exe.win-amd64-3.3\python33.dll

Traceback (most recent call last):
  File "setup.py", line 17, in <module>
    executables = [Executable("send_email.py", icon="icon.ico", base=base)])
  File "c:\Python33\lib\site-packages\cx_Freeze\dist.py", line 365, in setup
    distutils.core.setup(**attrs)
  File "c:\Python33\lib\distutils\core.py", line 148, in setup
    dist.run_commands()
  File "c:\Python33\lib\distutils\dist.py", line 917, in run_commands
    self.run_command(cmd)
  File "c:\Python33\lib\distutils\dist.py", line 936, in run_command
    cmd_obj.run()
  File "c:\Python33\lib\distutils\command\build.py", line 126, in run
    self.run_command(cmd_name)
  File "c:\Python33\lib\distutils\cmd.py", line 313, in run_command
    self.distribution.run_command(command)
  File "c:\Python33\lib\distutils\dist.py", line 936, in run_command
    cmd_obj.run()
  File "c:\Python33\lib\site-packages\cx_Freeze\dist.py", line 235, in run
    freezer.Freeze()
  File "c:\Python33\lib\site-packages\cx_Freeze\freezer.py", line 577, in Freeze

    self._FreezeExecutable(executable)
  File "c:\Python33\lib\site-packages\cx_Freeze\freezer.py", line 169, in _Freez
eExecutable
    cx_Freeze.util.AddIcon(exe.targetName, exe.icon)
SystemError: error return without exception set

【问题讨论】:

我以前没见过那个。你能file an issue 这样它就不会被遗忘吗? 【参考方案1】:

我遇到了同样的消息错误,我通过提供图标文件的完整路径来修复它。 顺便说一句,确保图标是 .ico 格式(起初我将 .png 文件的扩展名重命名为 .ico 并导致进程崩溃,最后我将 .png 文件转换为 .ico 格式并且它工作)。

【讨论】:

转换为 .ico 为我解决了这个问题【参考方案2】:

使用这个设置文件。

from cx_Freeze import setup, Executable

GUI2Exe_Target_1 = Executable(
    script = "Your scripts",
    initScript = None,
    base = 'Win32GUI',
    targetName = "app.exe",
    compress = True,
    copyDependentFiles = True,
    appendScriptToExe = False,
    appendScriptToLibrary = False,
    icon = "YOUR ICON FILE.ico"
    ) 
excludes = ["pywin", "tcl", "pywin.debugger", "pywin.debugger.dbgcon",
        "pywin.dialogs", "pywin.dialogs.list", "win32com.server",
        "email"] 
includes = ["PyQt4.QtCore","PyQt4.QtGui","win32gui","win32com","win32api","html.parser","sys","threading","datetime","time","urllib.request","re","queue","os"] 
packages = [] 
path = [] 
setup(
    version = "1.0",
    description = "myapp",
    author = "me",
    author_email = "email@email.com",
    name = "Your app name !",
    options = "build_exe": "includes": includes,
                             "excludes": excludes,
                             "packages": packages,
                             "path": path
                            
               ,
    executables = [GUI2Exe_Target_1]
    )

【讨论】:

【参考方案3】:

我遇到了刚刚修复的相同错误!有一个非常简单的解决方案,由于您的图标文件,您会收到此错误。

    我认为你有一个 setup.py 文件,其中包含executables = [cx_Freeze.Executable("filename.py", base=base, icon="youricon")] 的内容

您需要确保您的图标是 .ico 文件。只需搜索 .gif 或 .png 到 .ico 转换器,它就会为您完成!

还要确保您的 .ico 文件位于您的文件文件夹中。 确保在选项中包含文件

您的 setup.py 中可能还有其他内容...

options = "build_exe":"packages":["THEMODS YOU IMPORTED HERE"],"include_files":["THE FILES NEED TO BE HERE"]

这就是为我解决问题的原因。 LMK 如果这有帮助:)

【讨论】:

【参考方案4】:

将您的选项更改为:

build_exe_options = "packages": ["os"], "excludes": ["tkinter"],"include_files": ["icon.ico"],

【讨论】:

以上是关于使用 cx_freeze 将 .py 转换为可执行文件时出错的主要内容,如果未能解决你的问题,请参考以下文章

将 python 文件转换为可执行文件时,出现“没有名为 cx_freeze.util 的模块”错误

无法使用 virtualenv 和 cx_Freeze 将 pygame 转换为可执行文件

尝试使用 cx_freeze 将 python 文件转换为可执行文件时出错

类型错误:列表索引必须是整数或切片,而不是 str 尝试使用 cx_Freeze 将 .py 文件转换为 .exe 时

使 cx_Freeze main.py 永久能够使用 numpy 模块

图像目录和cx_Freeze