无法使用cx_freeze为python脚本创建和运行独立可执行文件

时间:2012-10-09 06:52:25

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

我的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>

2 个答案:

答案 0 :(得分:2)

构建完成后,将re.pyc添加到library.zip文件中 要获得re.pyc,您需要做的就是成功运行re.py,然后打开__pycache__文件夹,然后您将看到像re.cpython-32.pyc这样的文件,将其重命名为re.pyc和voila !

答案 1 :(得分:1)

<强> 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")])