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

How to compile executable with cx_Freeze with modules in Python 3.x

这是我第一次post来这里,所以如果我有错误请告诉我,我会更正。我在 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 不工作:

我将 post 错误代码 pastebin

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

您需要设置环境变量 TCL_DIRECTORYTK_DIRECTORY 并告诉 cx_Freeze 使用 build_exe 选项包含 Tcl 和 Tk DLL include_files. If you are using cx_Freeze 5.1.1 or 5.1.0, you need to do it slightly differently, see .

中完成

此外,您应该为 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)