使用 cx_Freeze 从.py构建.exe文件



我正在尝试从python上的项目制作可执行文件(setup.py)

import sys
import xlrd
import pyodbc
import tkinter as tk
import os.path
from PyQt5.QtCore import pyqtSlot
from PyQt5.QtWidgets import QApplication,QDialog
from PyQt5.uic import loadUi
from PyQt5 import QtGui
from tkinter import filedialog
from datetime import datetime, timedelta
from win32api import GetSystemMetrics
from cx_Freeze import setup, Executable
base = None    
PYTHON_INSTALL_DIR = os.path.dirname(os.path.dirname(os.__file__))
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')
#os.environ['TCL_LIBRARY'] = r'C:UsersPQ946KKAppDataLocalProgramsPythonPython37tcltcl8.6'
#os.environ['TK_LIBRARY'] = r'C:UsersPQ946KKAppDataLocalProgramsPythonPython37tcltk8.6'
executables = [Executable("Load File.py",
base=base,
icon="BI Icon.ico")] 
packages = ["tkinter","pyodbc","PyQt5","datetime","win32api"]
files = ["BI Icon.ico","Load Excel.ui",
r"C:UsersPQ946KKDocumentsLetiProyectosPowerBIIconarrow up load.png",
os.path.join(PYTHON_INSTALL_DIR, 'DLLs', 'tk86t.dll'),
os.path.join(PYTHON_INSTALL_DIR, 'DLLs', 'tcl86t.dll')]
options = {
'build_exe': {    
'packages':packages,
'include_files':files,
},    
}
setup(
name = "LoadFile BI",
options = options,
version = "0.1",
description = 'Ejecutable de Aplicacion Load File',
executables = executables
)

每当我构建时,它只会生成几个文件(只要我记得它应该创建更多的文件和文件夹)

(base) C:UsersXXXXXXDocumentsLetiProyectosPowerBI>python setup.py build
running build
running build_exe
copying C:UsersPQ946KKAppDataLocalContinuumanaconda3libsite-packagescx_FreezebasesConsole.exe -> buildexe.win-amd64-3.6Load File.exe
copying C:UsersPQ946KKAppDataLocalContinuumanaconda3api-ms-win-crt-stdio-l1-1-0.dll -> buildexe.win-amd64-3.6api-ms-win-crt-stdio-l1-1-0.dll
copying C:UsersPQ946KKAppDataLocalContinuumanaconda3python36.dll -> buildexe.win-amd64-3.6python36.dll
copying C:UsersPQ946KKAppDataLocalContinuumanaconda3VCRUNTIME140.dll -> buildexe.win-amd64-3.6VCRUNTIME140.dll
copying C:UsersPQ946KKAppDataLocalContinuumanaconda3api-ms-win-crt-math-l1-1-0.dll -> buildexe.win-amd64-3.6api-ms-win-crt-math-l1-1-0.dll
copying C:UsersPQ946KKAppDataLocalContinuumanaconda3api-ms-win-crt-locale-l1-1-0.dll -> buildexe.win-amd64-3.6api-ms-win-crt-locale-l1-1-0.dll
copying C:UsersPQ946KKAppDataLocalContinuumanaconda3api-ms-win-crt-string-l1-1-0.dll -> buildexe.win-amd64-3.6api-ms-win-crt-string-l1-1-0.dll
copying C:UsersPQ946KKAppDataLocalContinuumanaconda3api-ms-win-crt-runtime-l1-1-0.dll -> buildexe.win-amd64-3.6api-ms-win-crt-runtime-l1-1-0.dll
copying C:UsersPQ946KKAppDataLocalContinuumanaconda3api-ms-win-crt-convert-l1-1-0.dll -> buildexe.win-amd64-3.6api-ms-win-crt-convert-l1-1-0.dll
copying C:UsersPQ946KKAppDataLocalContinuumanaconda3api-ms-win-crt-time-l1-1-0.dll -> buildexe.win-amd64-3.6api-ms-win-crt-time-l1-1-0.dll
copying C:UsersPQ946KKAppDataLocalContinuumanaconda3api-ms-win-crt-environment-l1-1-0.dll -> buildexe.win-amd64-3.6api-ms-win-crt-environment-l1-1-0.dll
copying C:UsersPQ946KKAppDataLocalContinuumanaconda3api-ms-win-crt-process-l1-1-0.dll -> buildexe.win-amd64-3.6api-ms-win-crt-process-l1-1-0.dll
copying C:UsersPQ946KKAppDataLocalContinuumanaconda3api-ms-win-crt-heap-l1-1-0.dll -> buildexe.win-amd64-3.6api-ms-win-crt-heap-l1-1-0.dll
copying C:UsersPQ946KKAppDataLocalContinuumanaconda3api-ms-win-crt-conio-l1-1-0.dll -> buildexe.win-amd64-3.6api-ms-win-crt-conio-l1-1-0.dll
copying C:UsersPQ946KKAppDataLocalContinuumanaconda3api-ms-win-crt-filesystem-l1-1-0.dll -> buildexe.win-amd64-3.6api-ms-win-crt-filesystem-l1-1-0.dll
(base) C:UsersXXXXXXDocumentsLetiProyectosPowerBI>python setup.py build

我尝试重新安装 Python、Anaconda 并设置全局变量,没有运气仍然得到相同的结果,当然.exe文件不起作用。错误信息:

致命的 Python 错误:Py_Initialize:无法加载文件系统编解码器 ModuleNotFoundError:没有名为"encodings"的模块

我还应该提到,这是我第一次使用单独的 .ui 文件构建文件。

您是否
  1. 尝试过将encodings添加到packages列表选项中?

    packages = ["tkinter","pyodbc","PyQt5","datetime","win32api","encodings"]
    
  2. 另请注意,如果使用版本 5.1.1
  3. (当前默认版本)或 5.1.0 冻结应用程序cx_Freeze则需要将 TCL 和 TK DLL 包含在子目录中lib。您可以通过将元组(source, destination)传递给include_files列表选项的相应条目来执行此操作:

    files = ["BI Icon.ico", "Load Excel.ui",
    r"C:UsersPQ946KKDocumentsLetiProyectosPowerBIIconarrow up load.png",
    (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'))]
    
  4. 如果构建 GUI 应用程序(看起来),则在Executable中使用它之前,还应在安装脚本中修改base的定义,如下所示:

    # GUI applications require a different base on Windows (the default is for a console application).
    base = None
    if sys.platform == "win32":
    base = "Win32GUI"
    
  5. 为什么你实际上同时使用tkinterPyQt5?一个应该足以提供一个 GUI,不是吗?如果您不在应用程序中使用tkinter(正如我猜的那样),您通常需要通过excludes列表选项在安装脚本中显式排除它(当然,将其从packages列表中删除并删除import tkinter as tk):

    packages = ["pyodbc","PyQt5","datetime","win32api"]
    files = ...
    excludes = ['tkinter']
    options = {
    'build_exe': {    
    'packages':packages,
    'include_files':files,
    'excludes':excludes
    },    
    }
    

    那时,您可能不再需要提供 TK 和 TCL DLL。

  6. 使用单独的.ui文件不是cx_Freeze的问题。您应该只确保冻结的应用程序在正确的位置查找.ui文件。

最新更新