[英]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: 我使用以下安装文件使用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")])
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. 我最初在将numpy包含为可识别的库时遇到了一个错误,但是我使用此链接的建议(将numpy驱动程序文件中的
_methods
文件复制到本地build/my.exe.../lib/numpy/core
)解决了这个问题(所以我认为) build/my.exe.../lib/numpy/core
目录。 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. 供参考:我有一台MacBook,但所有这些都在计算机的Windows分区上运行。 I also have a Conda distribution with Python3.
我也有Python3的Conda发行版。
Does your Windows partition have a Windows operating system or only a Windows file system? 您的Windows分区具有Windows操作系统还是只有Windows文件系统? 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) 我还将在整个
numpy
和pandas
packages
选项,而不是仅包含模块( 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']
},
}
I'm not sure whether you need to explicitly include the other modules at all, or with the packages
option as well. 我不确定您是否需要显式地包括其他模块,或者还需要使用
packages
选项。
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. 如果这些建议仍不能解决问题,建议您从cx_Freeze / samples / Tkinter中的示例开始,并一一添加应用程序的不同组件(GUI组件,包括,pandas / numpy等)。同时在每个步骤之间测试可执行文件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.