【问题标题】:Py2exe creates program that dosen't workPy2exe创建不起作用的程序
【发布时间】:2014-03-08 04:52:23
【问题描述】:

我在我的程序中使用了 Py2exe:

import time

start = time.clock()

def sortare( n ):
    return sorted(str(n))

def main():
    n = 99999
    while True:
        if sortare (2 * n) == sortare(3 * n) == sortare(4 * n) == sortare(5 * n) == sortare(6 * n):
            print (n)
            break
        n += 1
    print (time.clock() - start)

if __name__ == "__main__":
    main()

这是我的 setup.py:

from distutils.core import setup
import py2exe

setup(windows=['C:\Users\Rares\workspace\Test\src\Test.py'])

我已经将 python 2.7.6 和 py2exe 安装为 64 位,因为我的机器是这样的。我在 Windows CMD 中使用
python C:\Python27\Test\test.py py2exe

我有一个名为 distr 我的程序所在的文件夹,但是当我运行 exe 时,没有任何反应。甚至没有错误或 txt 文件。

感谢您的任何建议。

【问题讨论】:

  • 你双击exe运行了吗?它可能运行和退出速度如此之快,你什么也看不到。您是否尝试过从命令行运行 exe?
  • 我确实遇到了一些只打开和关闭的 exe,但是我使用的这个算法至少需要 1 秒才能工作。即便如此,绝对没有图形提示程序已经运行。同样从 CMD 运行它,它只显示一个空白行。

标签: python python-2.7 py2exe


【解决方案1】:

尝试在脚本的第一两行之一中添加 shebang #! 行。

在这里查看更多信息:What does the symbol "#!" mean in Python?

编辑:根据您使用的 python 版本(由python -V 验证),shebang 行将是这样的:

#! /usr/local/bin/python2.7

【讨论】:

  • 不是 UNIX 的 shebang 吗?我正在运行 windows,即使我使用它仍然是同样的问题。
  • @user3232848 我在网上找到的一个新手解决方案可能会有所帮助,那就是代码末尾的raw_input("Press<enter>")。还可以尝试在代码中添加更多打印语句,以获取代码读取过程的状态。
  • @user3232848 也适用于shebang,您对它适用于unix,但是您的程序可能也必须在其他操作系统中运行,因此包含它是一个好习惯。如果您有任何问题,请随时提问。
【解决方案2】:

不要在 setup.py 中使用“windows=”作为选项(用于创建 Windows GUI 应用程序),您需要使用“console=”来创建 Windows 控制台应用程序(它将打印内容到控制台)。

我将您的 setup.py 更改为如下,它对我有用:

from distutils.core import setup
import py2exe

setup(console=[{'script':'C:\Users\Rares\workspace\Test\src\Test.py'}])

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-30
    • 1970-01-01
    • 2021-10-11
    相关资源
    最近更新 更多