【问题标题】:Unable to create and run standalone executable for python script using cx_freeze无法使用 cx_freeze 创建和运行 Python 脚本的独立可执行文件
【发布时间】:2012-09-29 10:47:09
【问题描述】:

我的 c:\Python32 中有 2 个 python 脚本 1)Tima_guess.py 看起来像这样:

#Author:Roshan Mehta
#Date  :9th October 2012

import random,time,sys
ghost ='''
0000 0 000----
00000-----0-0
----0000---0
'''

guess_taken = 0

print('Hello! What is your name?')
name = input()
light_switch = random.randint(1,12)
print("Well, " + name + ", There are 12 switches and one of them turns on the Light.\nYou just need to guess which one is it in 4 guesses.Purely a luck,but i will help you to choose")
print("Choose a switch,they are marked with numbers from 1-12.\nEnter the switch no.") 
while guess_taken < 4:

    try:
        guess = input()
        guess = int(guess)
    except: 
        print("invalid literal,Plese enter an integer next time.")
        x = input()
        sys.exit(1)


    guess_taken = guess_taken + 1
    guess_remain = 4 - guess_taken
    time.sleep(1)

    if guess < light_switch:        
        print("The Light's switch is on right of your current choice.You have {} more chances to turn on the light.".format(guess_remain)) 

    if guess > light_switch:
        print("The Light's switch is on left of your current choice.You have {} more chances to turn on the light.".format(guess_remain))

    if guess == light_switch:
        print("Good,you are quiet lucky,You have turned on the light in {} chances.".format(guess_taken))
        sys.exit(1)

if guess != light_switch:
    print("Naah,You don't seems to be lucky enough,The switch was {}.".format(light_switch))
    for i in range(3):
        time.sleep(2)
        print(ghost)
    print("The Devil in the room has just killed you....Ha ha ha")

input()

2)setup.py 如下所示:

from cx_Freeze import setup, Executable
setup(
    name = "Console game",
    version = "0.1",
    description = "Nothing!",
    executables = [Executable("Tima_guess.py")])

当我运行 python setup.py build 时,它会在 c:\Python32\build 内的 build 目录中创建一个可执行文件,但是当我运行 Tima_guess.exe 时,它​​只会显示黑屏并立即熄灭,甚至无法看到它的消息正在投掷。 请帮助我获取我的 Tima_guess.py 游戏的独立可执行文件。

问候。

根据 Thomas 的建议,当我通过 Tima_guess.exe 明确地在 cmd 中运行时。我收到以下错误,但仍然无法找出问题所在。

c:\Python32\build\exe.win32-3.2>Tima_guess.exe
Traceback (most recent call last):
  File "c:\Python32\lib\site-packages\cx_Freeze\initscripts\Console3.py", line 2
7, in <module>
    exec(code, m.__dict__)
  File "Tima_guess.py", line 4, in <module>
  File "c:\Python32\lib\random.py", line 39, in <module>
    from warnings import warn as _warn
  File "C:\Python\32-bit\3.2\lib\warnings.py", line 6, in <module>
  File "C:\Python\32-bit\3.2\lib\linecache.py", line 10, in <module>
  File "C:\Python\32-bit\3.2\lib\tokenize.py", line 28, in <module>
ImportError: No module named re

c:\Python32\build\exe.win32-3.2>

【问题讨论】:

  • 您可以尝试在命令提示符下运行Tima_guess.exe,以便查看它产生的错误消息吗?
  • 是的,它只是运行然后熄灭......什么都看不到。如果我只是在 IDLE 中运行 Tima_guess.py。它工作得很好,但我想要独立的 .exe,我可以分发到我的朋友们。
  • 如果您在命令提示符下进入该文件夹并键入Tima_guess.exe,您应该能够运行它,这样它就不会立即消失,并且您可以看到错误消息。您需要查看错误消息才能找出问题所在。
  • 谢谢Thomas...但我仍然无法找出问题所在。请帮帮我。
  • 好的,我想你只需要指定它应该包括re。在此处查看示例 setup.py:cx_freeze.readthedocs.org/en/latest/distutils.html

标签: python-3.x executable cx-freeze


【解决方案1】:

构建完成后,将 re.pyc 添加到 library.zip 文件中。
要得到re.pyc,你需要做的就是成功运行re.py,然后打开__pycache__文件夹,然后你会看到一个类似re.cpython-32.pyc的文件,把它重命名为re.pyc,瞧!

【讨论】:

    【解决方案2】:

    setup.py

    from cx_Freeze import setup, Executable
    
    build_exe_options = {"includes": ["re"]}
    
    setup(
            name = "Console game",
            version = "0.1",
            description = "Nothing!",
            options = {"build_exe": build_exe_options},
            executables = [Executable("Tima_guess.py")])
    

    【讨论】:

    • 伙计们,我已将此行添加到我的 setup.py 但我仍然遇到相同的错误,如果可能,请尝试在您的系统中使 Tima_guess 可执行并让我知道它是否正常工作? build_exe_options = {"includes": ["re"]}
    猜你喜欢
    • 2016-01-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-19
    • 1970-01-01
    • 2010-09-16
    • 1970-01-01
    相关资源
    最近更新 更多