【问题标题】:How do I use cx_freeze?如何使用 cx_freeze?
【发布时间】:2012-04-11 08:15:51
【问题描述】:

我已按照说明创建了 setup.py 文件,但实际上我并没有……了解下一步该做什么。在命令行中输入“python setup.py build”只会出现语法错误。

那么,我该怎么办?

setup.py:

from cx_Freeze import setup, Executable

setup(
    name = "On Dijkstra's Algorithm",
    version = "3.1",
    description = "A Dijkstra's Algorithm help tool.",
    exectuables = [Executable(script = "Main.py", base = "Win32GUI")])

【问题讨论】:

  • 你能告诉我们setup.py,你的python版本吗?
  • @birryree:查看已编辑的问题。
  • 什么是语法错误?可以发回溯吗?
  • @aquavitae 它在命令行中。有人可以说清楚我应该把“python setup.py build”放在哪里,因为我读过的说明没有说清楚。
  • 我认为您的问题是exectuables 拼写错误。应该是executables

标签: python cx-freeze


【解决方案1】:

我真的不确定你在做什么来得到那个错误,看起来你正在尝试自己运行 cx_Freeze,没有参数。因此,这里有一个简短的分步指南,介绍如何在 Windows 中执行此操作(您的屏幕截图看起来很像 Windows 命令行,所以我假设这是您的平台)

  1. 编写您的 setup.py 文件。您上面的脚本看起来是正确的,所以它应该可以工作,假设您的脚本存在。

  2. 打开命令行(Start -> Run -> "cmd"

  3. 转到 setup.py 文件所在的位置并运行 python setup.py build

注意事项:

  1. 您的脚本名称可能有问题。 “Main.py”包含大写字母,这可能会引起混淆,因为 windows 的文件名不区分大小写,但 python 是。我的方法是始终为脚本使用小写以避免任何冲突。

  2. 确保 python 在你的 PATH 中(阅读http://docs.python.org/using/windows.html1

  3. 确保正在查看新的 cx_Freeze documentation。 Google 似乎经常会调出旧文档。

【讨论】:

  • "'python' 不是内部或外部命令、可运行程序或批处理文件。"
  • "python: can't open file 'setup.py': [Errno 2] No such file or directory" 键入 python 会按预期得到 Python 命令提示符,所以我不明白那个错误.我的文件在 Python31 文件夹中。
  • 使用cd 切换到您的setup.py 文件所在的目录。
  • @ThomasK:做到了!我以前从未真正使用过命令行,所以我需要像小孩子一样指导它。感谢大家的帮助。
【解决方案2】:

我遇到了类似的问题。我通过在变量中设置可执行选项然后简单地调用变量来解决它。下面是我使用的示例 setup.py:

from cx_Freeze import setup, Executable
import sys

productName = "ProductName"
if 'bdist_msi' in sys.argv:
    sys.argv += ['--initial-target-dir', 'C:\InstallDir\\' + productName]
    sys.argv += ['--install-script', 'install.py']

exe = Executable(
      script="main.py",
      base="Win32GUI",
      targetName="Product.exe"
     )
setup(
      name="Product.exe",
      version="1.0",
      author="Me",
      description="Copyright 2012",
      executables=[exe],
      scripts=[
               'install.py'
               ]
      ) 

【讨论】:

    【解决方案3】:
    • import sys 添加为新的顶行
    • 您在最后一行拼错了“executables”。
    • 删除最后一行的script =

    代码现在应该如下所示:

    import sys
    from cx_Freeze import setup, Executable
    
    setup(
        name = "On Dijkstra's Algorithm",
        version = "3.1",
        description = "A Dijkstra's Algorithm help tool.",
        executables = [Executable("Main.py", base = "Win32GUI")])
    

    使用命令提示符 (cmd) 运行 python setup.py build。 (从包含setup.py 的文件夹中运行此命令。)注意我们在脚本调用末尾添加的build 参数。

    【讨论】:

    • base 的目的是什么,如果我想让我的可执行文件跨多个平台运行怎么办?
    【解决方案4】:

    你可以把 setup.py 代码改成这样:

        from cx_freeze import setup, Executable
        setup( name = "foo",
               version = "1.1",
               description = "Description of the app here.",
               executables = [Executable("foo.py")]
             )
    

    我相信它会起作用。我在 Windows 7 和 ubuntu 12.04 上都试过了

    【讨论】:

      【解决方案5】:

      找到cxfreeze 脚本并运行它。它将与您的其他 python 帮助程序脚本位于同一路径中,例如 pip

      cxfreeze Main.py --target-dir dist

      阅读更多: http://cx-freeze.readthedocs.org/en/latest/script.html#script

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2023-01-04
        • 2015-06-13
        • 1970-01-01
        • 1970-01-01
        • 2011-02-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多