简体   繁体   English

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

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

This is my first post here, so if I make a mistake please tell me, I'll correct it. 这是我在这里的第一篇文章,因此,如果我输入有误,请告诉我,我会予以纠正。 I am in python 3.6, windows 10, I have a program that I need to compile with cx_Freeze . 我在python 3.6,Windows 10中,我有一个需要使用cx_Freeze进行编译的cx_Freeze I cannot get my setup.py to work, it has an error when I try to compile. 我无法运行setup.py ,尝试编译时出现错误。 The program I am trying to compile starts with: 我要编译的程序开始于:

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

I need all of these to make the program work, yet I need to compile it with cx_Freeze , Somebody please help me! 我需要所有这些来使程序正常工作,但是我需要使用cx_Freeze进行编译,请有人帮助我!

My setup.py is 我的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)

I have a compiler.bat that contains: 我有一个包含以下内容的compiler.bat:

python setup.py build

And my error is: Powershell Error 我的错误是: Powershell错误

Seems like I cannot insert images yet I need a reputation. 好像我无法插入图像,但我需要声誉。

PyInstaller does not work: PyInstaller不起作用:

I will post error code on pastebin 我将在pastebin上发布错误代码

If there is a solution to the problem with py2exe(or whatever variation of that compiler), please tell me just keep in mind that I am in python 3. 如果有py2exe(或该编译器的任何变体)问题的解决方案,请告诉我,请记住我在python 3中。

You need to set the environment variables TCL_DIRECTORY and TK_DIRECTORY and to tell cx_Freeze to include the Tcl and Tk DLLs using the build_exe option include_files as done in this answer . 您需要设置环境变量TCL_DIRECTORYTK_DIRECTORY ,并使用build_exe选项include_files告诉cx_Freeze包括Tcl和Tk DLL,如本答案所述 If you are using cx_Freeze 5.1.1 or 5.1.0, you need to do it slightly differently, see this answer . 如果您使用的是cx_Freeze 5.1.1或5.1.0,则需要稍有不同,请参见此答案

Furthermore, you should set base = "Win32GUI" for GUI applications under Windows. 此外,您应该为Windows下的GUI应用程序设置base = "Win32GUI"

In summary, assuming you are using cx_Freeze 5.1.1 (the current version), try to use the following setup script: 总之,假设您正在使用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)

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

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