cx_Freeze之后,Python可执行文件将无法运行

时间:2018-08-20 22:00:24

标签: python build executable cx-freeze

我正在尝试创建一个独立程序,但在使最终输出实际正常运行方面遇到困难。我的实际代码是专有的,但我认为以下示例可能是一个不错的工作示例:

import pandas as pd
import tkinter as tk
from tkinter import filedialog
#from datetime import datetime

# GUI
#------------------------------------------------------------------------------
# Window if no changes
def window_if_empty(self,message):
    win = tk.Tk()
    win.geometry("400x300")
    win.title("Empty Output")
    Message = tk.Label(win, text = message)
    Message.pack()
    Close = tk.Button(win, text = "Close Window", command = win.destroy)
    Close.pack(anchor = "center")
    win.mainloop()

class Main(object):
    def __init_(self):
        pass

    # Create Main Page
    def MainPage(self,root):
        root.title("Test GUI")

        # Button for tracking changes
        track_changes = tk.Button(root, text = "Identify Changes Between Spreadsheets", command = self.Track_Changes)
        track_changes.place(relx = 0.5, rely = 0.4, anchor = "center")

    def Track_Changes(self):
        name_1 = filedialog.askopenfilename(initialdir = "/", title = "Select file #1", filetypes = (("xlsx files","*.xlsx"),("csv files", "*.csv"),("all files","*.*")))
        name_2 = filedialog.askopenfilename(initialdir = "/", title = "Select file #2", filetypes = (("xlsx files","*.xlsx"),("csv files", "*.csv"),("all files","*.*")))

        # Import File #1
        if name_1.endswith('.csv'):
            self.File_1 = pd.read_csv(name_1)
        elif name_1.endswith('.xlsx'):
            self.File_1 = pd.read_excel(name_1)

        # Import File #2
        if name_2.endswith('.csv'):
            self.File_2 = pd.read_csv(name_2)
        elif name_2.endswith('.xlsx'):
            self.File_2 = pd.read_excel(name_2)

        merged = self.File_1.merge(self.File_2, how = "outer", indicator = True)     
        old_data = merged[merged["_merge"] == "left_only"]
        old_data = old_data.reset_index(drop = True)
        new_data = merged[merged["_merge"] == "right_only"]
        new_data = new_data.reset_index(drop = True)

        self.Changed = new_data

        if self.Changed.empty:
            window_if_empty(self, "No changes tracked between LMS files.")
        else:
            save_name = filedialog.asksaveasfilename(defaultextension = ".csv", title = "Save Changes As...")   #.asksaveasfilename(mode = 'w', defaultextension = ".csv")
            self.Changed.to_csv(save_name, index = False)





# Run GUI
#------------------------------------------------------------------------------
root = tk.Tk()
root.geometry("500x500")
App  = Main()
App.MainPage(root)
root.mainloop()

我使用以下安装文件使用cx_Freeze构建:

from cx_Freeze import setup, Executable
import os

# Freeze
#------------------------------------------------------------------------------
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')

Options = {
    'build_exe': {
        'include_files':[
            os.path.join(PYTHON_INSTALL_DIR, 'DLLs', 'tk86t.dll'),
            os.path.join(PYTHON_INSTALL_DIR, 'DLLs', 'tcl86t.dll'),
         ], 'includes':['numpy', 'pandas', 'tkinter', 'datetime', 'atexit']
    }, 
}

setup(options = Options ,
      name = "Document Prep" ,
      version = "0.1" ,
      description = "" ,
      executables = [Executable("test_GUI.py", base = "Win32GUI")])

我最初在将numpy包含为可识别的库时出错,但是我使用this link here的建议(将numpy驱动程序文件中的_methods文件复制到我的本地build/my.exe.../lib/numpy/core目录。现在的问题是,当我双击可执行文件时,什么也没有发生。什么都没开始,没有错误,我也不知道如何解决这个问题。任何帮助表示赞赏。

供参考:我有一台MacBook,但所有这些都在我计算机的Windows分区上运行。我也有Python3的Conda发行版。

1 个答案:

答案 0 :(得分:0)

您的Windows分区是否具有Windows操作系统或仅具有Windows文件系统?无论如何,您是否尝试按照here所述修改设置文件的末尾?

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

setup(options = Options ,
      name = "Document Prep" ,
      version = "0.1" ,
      description = "" ,
      executables = [Executable("test_GUI.py", base=base)])

这将使您的应用程序在任何情况下都更具可移植性。

我还将使用numpy选项包含整个软件包pandaspackages而不是仅包含模块(includes选项)

Options = {
    'build_exe': {
        'include_files':[
            os.path.join(PYTHON_INSTALL_DIR, 'DLLs', 'tk86t.dll'),
            os.path.join(PYTHON_INSTALL_DIR, 'DLLs', 'tcl86t.dll'),
         ], 
         'packages':['numpy', 'pandas'],
         'includes':['tkinter', 'datetime', 'atexit']
    }, 
}

我不确定您是否需要显式地包括其他模块,或者还需要使用packages选项。

如果这些建议仍不能解决问题,我建议您从cx_Freeze / samples / Tkinter中的示例开始,并添加应用程序的不同组件(GUI组件,包括,pandas / numpy等)。一步步地测试可执行文件。