【问题标题】:Python Executable Won't Run After cx_FreezePython 可执行文件在 cx_Freeze 之后不会运行
【发布时间】:2019-01-27 01:43:35
【问题描述】:

我正在尝试创建一个独立程序,但无法让最终输出真正正常运行。我的实际代码是专有的,但我认为以下是一个不错的工作示例:

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 的建议解决了这个问题(我认为),将 _methods 文件从 numpy 驱动程序文件复制到我的本地 @987654325 @ 目录。我现在的问题是,当我双击可执行文件时,什么也没有发生。什么都没有开始,没有错误,我不知道如何解决这个问题。任何帮助表示赞赏。

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

【问题讨论】:

    标签: python build executable cx-freeze


    【解决方案1】:

    您的 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)])
    

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

    我还将包含整个包 numpypandaspackages 选项,而不仅仅是模块(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...)在每个步骤之间测试可执行文件。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-06-02
      • 1970-01-01
      • 1970-01-01
      • 2021-08-28
      • 2018-01-06
      • 1970-01-01
      • 2019-04-03
      • 2020-04-10
      相关资源
      最近更新 更多