简体   繁体   English

适用于我的python脚本的CX_Freeze —修复模块错误?

[英]CX_Freeze for my python script — Fix up module error?

This is my main code: 这是我的主要代码:

import turtle
import random
from sys import exit
import canvasvg
import os
import tempfile
import shutil
import cairosvg

red = 125
green = 70
blue = 38        
pen = 15
def runChk():
    runAgain = input("Would you like to return to menu? Y/N (N will exit) \n")
    if runAgain.upper() == "Y":
        print("Running...")
        turtle.clearscreen()
        start()
    elif runAgain.upper() == "N":
        print("Exiting...")
        exit()
    else:
        print("Invalid response.")
        runChk()

def saveImg():
    print("Done.")
    save = input("Would you like to save this tree? Y/N \n")
    if save.upper() == "Y":
        filename = input("What would you like to name it? \n")
        print("Ignore following warning...")
        if not filename.lower().endswith('.png'):
            filename += '.png'
        target_path = os.path.join(os.path.abspath(os.path.dirname(__file__)), 'Images', filename)
        tmpdir = tempfile.mkdtemp()                   # create a temporary directory
        tmpfile = os.path.join(tmpdir, 'tmp.svg')    # name of file to save SVG to
        ts = turtle.getscreen().getcanvas()
        canvasvg.saveall(tmpfile, ts)
        with open(tmpfile) as svg_input, open(target_path, 'wb') as png_output:
            cairosvg.svg2png(bytestring=svg_input.read(), write_to=png_output)
        shutil.rmtree(tmpdir)     # clean up temp file(s)
        print("Saved!")
        runChk()
    elif save.upper() == "N":
        runChk()
    else:
        print("Invalid response. \n")
        saveImg()

def tree(branchLen, red, green, blue, pen):
    if branchLen > 3:
        pen = pen*0.8
        turtle.pensize(pen)
        if (red > 10 and green < 140):
            red = red - 15
            green = green + 8
    if branchLen > 5:
        angle = random.randrange(18, 55)
        angleTwo = 0.5*angle
        sub = (0.8*(random.randrange(1,20)))
        print("Angle 1: ", angle, "Angle 2: ", angleTwo, " Branch length subtracted by ", sub)
        turtle.color(red, green, blue)
        turtle.forward(branchLen)
        turtle.right(angleTwo)
        tree(branchLen-sub, red, green, blue, pen)
        turtle.left(angle)
        tree(branchLen-sub, red, green, blue, pen)
        turtle.right(angleTwo)
        turtle.backward(branchLen)
def main():
    turtle.colormode(255)
    turtle.bgcolor(102, 255, 255)
    turtle.left(90)
    turtle.up()
    turtle.speed(0)
    turtle.hideturtle()
    turtle.backward(440)
    turtle.down()
    print("Please wait while I draw...")
    tree(random.randrange(80, 95),red,green,blue, pen)
    turtle.update()
    saveImg()
def start():
    live = 1
    print("What would you like to do?\n")
    usr = input("Type 'run' to start a fractal. \nType 'run static' to create a fractal without live drawing (faster). \nOr 'exit' to exit. \n")
    if usr.upper() == "RUN":
        live = 1
        print("Running...")
        main()
    elif usr.upper() == "RUN STATIC":
        live = 0
        print("Running...")
        turtle.tracer(0)
        main()
    elif usr.upper() == "EXIT":
        print("Exiting...")
        exit()
    else:
        print("Invalid response.")
        start()
start()

it is called fractal.py. 它称为fractal.py。 I copied that and canvasvg to the main python folder. 我将其和canvasvg复制到了主python文件夹中。 I created a cx_freeze setup as so: 我创建了一个cx_freeze设置,如下所示:

from cx_Freeze import setup, Executable
# NOTE: you can include any other necessary external imports here aswell

includefiles = [] # include any files here that you wish
includes = []
excludes = []
packages = ['turtle', 'random', 'sys', 'canvasvg', 'os', 'tempfile', 'shutil', 'cairosvg']

exe = Executable(
 # what to build
   script = "fractal.py", # the name of your main python script goes here 
   initScript = None,
   base = None, # if creating a GUI instead of a console app, type "Win32GUI"
   targetName = "Fractal Tree.exe", # this is the name of the executable file
   copyDependentFiles = True,
   compress = True,
   appendScriptToExe = True,
   appendScriptToLibrary = True,
   icon = None # if you want to use an icon file, specify the file name here
)

setup(
 # the actual setup & the definition of other misc. info
    name = "Fractal Tree", # program name
    version = "0.4.2",
    description = 'Creates a fractal tree',
    author = "TomSoft Programs",
    options = {"build_exe": {"excludes":excludes,"packages":packages,
      "include_files":includefiles}},
    executables = [exe]
)

When I run cmd as python setup.py build , I get no errors. 当我将cmd作为python setup.py build运行时,我没有收到任何错误。 I go to the build folder and find my fractal.exe program. 我转到生成文件夹并找到我的fractal.exe程序。 It closes very quickly and I can't get the full error (because of the speedy closing) but I know it says something like: 它关闭非常快,并且我无法得到完整的错误(由于快速关闭),但是我知道它显示类似以下内容:

ReferenceError: 'module' object has no attribute '_fix_up_module'

What can I do to fix this? 我该怎么做才能解决此问题? I'm very new to python and cx_freeze, so I'm sure I'm doing something wrong in setup.py. 我是python和cx_freeze的新手,所以我确定我在setup.py中做错了什么。

For anyone who finds this, it's a bug with cx_freeze. 对于任何发现此问题的人,这都是cx_freeze的错误。 You've done nothing wrong. 你没做错任何事。 Download cx_freeze from here , the bug is fixed at this download location. 此处下载cx_freeze, 错误已在此下载位置修复。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM