简体   繁体   中英

Python Executable Won't Run After cx_Freeze

I'm trying to create a standalone program but I'm having trouble getting the final output to actually run properly. My actual code is proprietary but I think the following can be a decent working example:

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()

I use a cx_Freeze build using the following setup file:

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")])

I originally had an error with including numpy as a recognizable library, but I fixed the issue (so I think) using a suggestion from this link here of copying the _methods file from the numpy driver files into my local build/my.exe.../lib/numpy/core directory. My issue now is that when I double click the executable file nothing happens. Nothing starts, there's no error, and I don't know how to troubleshoot this. Any help is appreciated.

For reference: I have a MacBook but I'm running all of this on a Windows partition on my machine. I also have a Conda distribution with Python3.

Does your Windows partition have a Windows operating system or only a Windows file system? Anyway, have you tried to modify the end of your setup file as described 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)])

This would make your application more portable in any case.

I also would include the whole packages numpy and pandas with the packages option instead of only the modules ( includes option)

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']
    }, 
}

I'm not sure whether you need to explicitly include the other modules at all, or with the packages option as well.

If these proposals still do not solve the problem, I would advise you to start from the example in cx_Freeze/samples/Tkinter and add the different components of your application (GUI components, includes, pandas/numpy, ...) one by one while testing the executable between each steps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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