如何在Python 3.x中使用cx_Freeze和模块编译可执行文件

时间:2018-11-12 19:48:55

标签: python python-3.x tkinter pygame cx-freeze

这是我在这里的第一篇文章,因此,如果我输入有误,请告诉我,我会予以纠正。我在python 3.6,Windows 10中,我有一个需要使用cx_Freeze进行编译的程序。我的setup.py无法正常工作,尝试编译时出现错误。我要编译的程序开始于:

import pygame
from pygame.locals import *
import sys
import time
import tkinter
from tkinter import filedialog
from tkinter import messagebox

我需要所有这些才能使程序正常工作,但是我需要使用cx_Freeze进行编译,请有人帮助我!

我的setup.py

from cx_Freeze import setup, Executable

base = None

executables = [Executable("to-compile.py", base=base)]

packages = ["idna","os","sys","tkinter","pygame"]
options = {'build_exe' : {'packages':packages}}

setup(name="<any name>",options=options,version="<any number>",description="<any description>",executables=executables)

我有一个compiler.bat,其中包含:

python setup.py build

我的错误是: Powershell Error

好像我无法插入图像,但我需要声誉。

PyInstaller不起作用:

我将在pastebin上发布错误代码

如果有解决py2exe(或该编译器的任何变体)问题的方法,请告诉我,请记住我在python 3中。

1 个答案:

答案 0 :(得分:0)

您需要设置环境变量TCL_DIRECTORYTK_DIRECTORY,并使用cx_Freeze选项build_exe告诉include_files包括Tcl和Tk DLL。在this answer中完成。如果您使用的是cx_Freeze 5.1.1或5.1.0,则需要稍微做些不同,请参见this answer

此外,您应该为Windows下的GUI应用程序设置base = "Win32GUI"

总而言之,假设您使用的是cx_Freeze 5.1.1(当前版本),请尝试使用以下安装脚本:

from cx_Freeze import setup, Executable

import os
import sys
PYTHON_INSTALL_DIR = os.path.dirname(sys.executable)
os.environ['TCL_LIBRARY'] = os.path.join(PYTHON_INSTALL_DIR, 'tcl', 'tcl8.6')
os.environ['TK_LIBRARY'] = os.path.join(PYTHON_INSTALL_DIR, 'tcl', 'tk8.6')

include_files = [(os.path.join(PYTHON_INSTALL_DIR, 'DLLs', 'tk86t.dll'), os.path.join('lib', 'tk86t.dll')),
                 (os.path.join(PYTHON_INSTALL_DIR, 'DLLs', 'tcl86t.dll'), os.path.join('lib', 'tcl86t.dll'))]
packages = ["idna","os","sys","tkinter","pygame"]
options = {'build_exe' : {'packages':packages, 'include_files':include_files}}

# GUI applications require a different base on Windows (the default is for a console application).
base = None
if sys.platform == "win32":
    base = "Win32GUI"

executables = [Executable("to-compile.py", base=base)]

setup(name="<any name>",options=options,version="0.1",description="<any description>",executables=executables)