使用cx_Freeze从.py生成.exe文件

时间:2018-12-04 15:12:28

标签: python-3.x user-interface pyqt5 cx-freeze

我正在尝试从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:\Users\PQ946KK\AppData\Local\Programs\Python\Python37\tcl\tcl8.6'
    #os.environ['TK_LIBRARY'] = r'C:\Users\PQ946KK\AppData\Local\Programs\Python\Python37\tcl\tk8.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:\Users\PQ946KK\Documents\Leti\Proyectos\PowerBI\Icon\arrow 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:\Users\XXXXXX\Documents\Leti\Proyectos\PowerBI>python setup.py build
    running build
    running build_exe
    copying C:\Users\PQ946KK\AppData\Local\Continuum\anaconda3\lib\site-packages\cx_Freeze\bases\Console.exe -> build\exe.win-amd64-3.6\Load File.exe
    copying C:\Users\PQ946KK\AppData\Local\Continuum\anaconda3\api-ms-win-crt-stdio-l1-1-0.dll -> build\exe.win-amd64-3.6\api-ms-win-crt-stdio-l1-1-0.dll
    copying C:\Users\PQ946KK\AppData\Local\Continuum\anaconda3\python36.dll -> build\exe.win-amd64-3.6\python36.dll
    copying C:\Users\PQ946KK\AppData\Local\Continuum\anaconda3\VCRUNTIME140.dll -> build\exe.win-amd64-3.6\VCRUNTIME140.dll
    copying C:\Users\PQ946KK\AppData\Local\Continuum\anaconda3\api-ms-win-crt-math-l1-1-0.dll -> build\exe.win-amd64-3.6\api-ms-win-crt-math-l1-1-0.dll
    copying C:\Users\PQ946KK\AppData\Local\Continuum\anaconda3\api-ms-win-crt-locale-l1-1-0.dll -> build\exe.win-amd64-3.6\api-ms-win-crt-locale-l1-1-0.dll
    copying C:\Users\PQ946KK\AppData\Local\Continuum\anaconda3\api-ms-win-crt-string-l1-1-0.dll -> build\exe.win-amd64-3.6\api-ms-win-crt-string-l1-1-0.dll
    copying C:\Users\PQ946KK\AppData\Local\Continuum\anaconda3\api-ms-win-crt-runtime-l1-1-0.dll -> build\exe.win-amd64-3.6\api-ms-win-crt-runtime-l1-1-0.dll
    copying C:\Users\PQ946KK\AppData\Local\Continuum\anaconda3\api-ms-win-crt-convert-l1-1-0.dll -> build\exe.win-amd64-3.6\api-ms-win-crt-convert-l1-1-0.dll
    copying C:\Users\PQ946KK\AppData\Local\Continuum\anaconda3\api-ms-win-crt-time-l1-1-0.dll -> build\exe.win-amd64-3.6\api-ms-win-crt-time-l1-1-0.dll
    copying C:\Users\PQ946KK\AppData\Local\Continuum\anaconda3\api-ms-win-crt-environment-l1-1-0.dll -> build\exe.win-amd64-3.6\api-ms-win-crt-environment-l1-1-0.dll
    copying C:\Users\PQ946KK\AppData\Local\Continuum\anaconda3\api-ms-win-crt-process-l1-1-0.dll -> build\exe.win-amd64-3.6\api-ms-win-crt-process-l1-1-0.dll
    copying C:\Users\PQ946KK\AppData\Local\Continuum\anaconda3\api-ms-win-crt-heap-l1-1-0.dll -> build\exe.win-amd64-3.6\api-ms-win-crt-heap-l1-1-0.dll
    copying C:\Users\PQ946KK\AppData\Local\Continuum\anaconda3\api-ms-win-crt-conio-l1-1-0.dll -> build\exe.win-amd64-3.6\api-ms-win-crt-conio-l1-1-0.dll
    copying C:\Users\PQ946KK\AppData\Local\Continuum\anaconda3\api-ms-win-crt-filesystem-l1-1-0.dll -> build\exe.win-amd64-3.6\api-ms-win-crt-filesystem-l1-1-0.dll

    (base) C:\Users\XXXXXX\Documents\Leti\Proyectos\PowerBI>python setup.py build

我尝试重新安装Python,Anaconda并设置全局变量,但是仍然无法获得相同的结果,当然.exe文件无法正常工作。错误消息:

  

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

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

1 个答案:

答案 0 :(得分:0)

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

    packages = ["tkinter","pyodbc","PyQt5","datetime","win32api","encodings"]
    
  2. 还请注意,如果使用cx_Freeze版本5.1.1(当前默认版本)或5.1.0冻结应用程序,则需要将TCL和TK DLL包含在子目录{{ 1}}。您可以通过将元组lib传递到(source, destination)列表选项的相应条目来做到这一点:

    include_files
  3. 如果您构建一个GUI应用程序(看起来如此),则还应在设置脚本中对files = ["BI Icon.ico", "Load Excel.ui", r"C:\Users\PQ946KK\Documents\Leti\Proyectos\PowerBI\Icon\arrow 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'))] 的定义进行如下修改,然后再在base中使用它:

    Executable
  4. 您为什么实际上同时使用# GUI applications require a different base on Windows (the default is for a console application). base = None if sys.platform == "win32": base = "Win32GUI" tkinter?提供一个GUI应该足够了,不是吗?如果您未在应用程序中使用PyQt5(据我所猜测),则通常需要借助tkinter列表选项在设置脚本中明确排除它(当然,也应将其从{ {1}}列表并删除excludes):

    packages

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

  5. 使用单独的import tkinter as tk文件对packages = ["pyodbc","PyQt5","datetime","win32api"] files = ... excludes = ['tkinter'] options = { 'build_exe': { 'packages':packages, 'include_files':files, 'excludes':excludes }, } 来说不是问题。您只应确保冻结的应用程序在正确的位置寻找.ui文件。