Python cx_freeze 4.3.4:设置targetName导致错误

Python cx_freeze 4.3.4: Setting targetName causes errors

我是 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 构建后我得到以下错误:

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 确实解决了这个问题

重新发布作为回答:

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

我运行用最新版的Cx_freeze解决了这个问题。

我发现我需要更改 setup.py 中的可执行文件调用以使用 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
    )