Python cx_freeze 4.3.4:设置targetName会导致错误

时间:2015-02-16 13:29:04

标签: python python-2.7 distutils cx-freeze

我是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确实可以解决此问题

2 个答案:

答案 0 :(得分:4)

重新作为答案:

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

答案 1 :(得分:3)

我使用最新版本的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
    )