【问题标题】:Python cx_freeze 4.3.4: Setting targetName causes errorsPython cx_freeze 4.3.4:设置 targetName 会导致错误
【发布时间】:2023-03-05 04:12:02
【问题描述】:

我对 cx_freeze 很陌生,我正在尝试更好地理解它,我有这个 setup.py 文件:

import sys
from cx_Freeze import setup, Executable

# Dependencies are automatically detected, but it might need fine tuning.
build_exe_options = {"packages": ["os"], "excludes": ["tkinter"]}

setup(  name = "guifoo",
        version = "0.1",
        description = "My GUI application!",
        options = {"build_exe": build_exe_options},
        executables = [Executable("mypy.py", base="Console", targetName="hello")])

如果我删除 targetName="hello" 它可以工作,但是当我包含它时,它不会。有人知道为什么吗?

这是我的python代码:

# encoding: utf8
import math
print "Starting..."
print math.sqrt(16)
input("please press enter to exit...")

运行 python setup.py build 后出现以下错误:

running build
running build_exe
creating directory build\exe.win32-2.7
copying C:\Python27\lib\site-packages\cx_Freeze\bases\Console.exe -> build\exe.win32-2.7\hello
copying C:\Windows\system32\python27.dll -> build\exe.win32-2.7\python27.dll
Traceback (most recent call last):
  File "setup.py", line 11, in <module>
    executables = [Executable("mypy.py", base="Console", targetName="hello")])
  File "C:\Python27\lib\site-packages\cx_Freeze\dist.py", line 362, in setup
    distutils.core.setup(**attrs)
  File "C:\Python27\lib\distutils\core.py", line 151, in setup
    dist.run_commands()
  File "C:\Python27\lib\distutils\dist.py", line 953, in run_commands
    self.run_command(cmd)
  File "C:\Python27\lib\distutils\dist.py", line 972, in run_command
    cmd_obj.run()
  File "C:\Python27\lib\distutils\command\build.py", line 127, in run
    self.run_command(cmd_name)
  File "C:\Python27\lib\distutils\cmd.py", line 326, in run_command
    self.distribution.run_command(command)
  File "C:\Python27\lib\distutils\dist.py", line 972, in run_command
    cmd_obj.run()
  File "C:\Python27\lib\site-packages\cx_Freeze\dist.py", line 232, in run
    freezer.Freeze()
  File "C:\Python27\lib\site-packages\cx_Freeze\freezer.py", line 621, in Freeze
    self._FreezeExecutable(executable)
  File "C:\Python27\lib\site-packages\cx_Freeze\freezer.py", line 211, in _FreezeExecutable
    self._AddVersionResource(exe.targetName)
  File "C:\Python27\lib\site-packages\cx_Freeze\freezer.py", line 150, in _AddVersionResource
    stamp(fileName, versionInfo)
  File "C:\Python27\lib\site-packages\win32\lib\win32verstamp.py", line 159, in stamp
    h = BeginUpdateResource(pathname, 0)
pywintypes.error: (2, 'BeginUpdateResource', 'The system cannot find the file specified.')

在目标名称处添加 .exe 可以解决此问题

【问题讨论】:

  • 您在 Windows 上吗?如果是这样,targetName 可能需要是“hello.exe”。顺便问一下,在问技术问题时,请不要只说它不起作用——究竟是什么不起作用?有错误信息吗?您的期望是什么,您得到的结果有何不同?
  • 是的,谢谢 Thomas,我会附上这些细节
  • 你能把它放在答案中以便我接受吗?

标签: python python-2.7 distutils cx-freeze


【解决方案1】:

作为答案转发:

targetName 是它要生成的可执行文件的文件名。在 Windows 上,可执行文件必须具有 .exe 扩展名,因此您需要将其设置为 'hello.exe' 而不仅仅是 'hello'

【讨论】:

    【解决方案2】:

    我在使用最新版本的 Cx_freeze 时遇到了这个问题。

    我发现我需要在 setup.py 中更改我的 Executable 调用以使用 dist 目录的相对路径。

    setup.py 中需要的更改

    来自

    MyExe_Target_1 = Executable(
        # what to build
        script = "main.py",
        initScript = None,
        base = None,
        targetDir = r"dist", 
        targetName = "MyWindowsApp.exe",
        compress = True,
        copyDependentFiles = True,
        appendScriptToExe = False,
        appendScriptToLibrary = False,
        icon = None
        )
    

    收件人:

    MyExe_Target_1 = Executable(
        # what to build
        script = "main.py",
        initScript = None,
        base = None,
        targetDir = r".\\dist",  # needs in Windows format relative to the working dir!
        targetName = "MyWindowsApp.exe",
        compress = True,
        copyDependentFiles = True,
        appendScriptToExe = False,
        appendScriptToLibrary = False,
        icon = None
        )
    

    【讨论】:

    • 我刚才遇到了同样的错误,这是唯一的解决方法(目前接受的答案是不够的)。此外,我最初没有targetDir,似乎它明确需要在 32 位 Windows 中定义它(对于 64 位省略它似乎有效)。
    猜你喜欢
    • 2016-07-05
    • 2015-12-16
    • 2013-02-14
    • 1970-01-01
    • 2011-06-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-18
    相关资源
    最近更新 更多