将我的Python代码放入exe后,它的代码无效

时间:2017-09-21 21:14:36

标签: python pandas python-3.6 cx-freeze

我想编写一个代码并创建一个GUI并使用python从中创建一个可执行文件。

我使用Tkinter进行GUI和pandas合并文件。这是代码:

from tkinter import *
from tkinter.filedialog import askdirectory
from tkinter import messagebox as mbox
import pandas as pd
import glob

def process_input_file():
    pass

class MyFrame(Frame):
  global input_file_path
  global output_folder_dest

  def __init__(self):
    Frame.__init__(self)
    self.master.title("Example")
    self.master.rowconfigure(5, weight=1)
    self.master.columnconfigure(5, weight=1)
    self.grid(sticky=W+E+N+S)

    Label(self, text='Input File Path').grid(row=0, column=0)
    a = StringVar()
    entry = Entry(self, textvariable=a).grid(row=0, column=5)
    self.button = Button(self, text="Browse", command=self.import_input_csv_data, width=10)
    self.button.grid(row=0, column=10, sticky=W)

    self.opLabel = Label(self, text='Output Destination Folder').grid(row=4, column=0)
    c = StringVar()
    entry = Entry(self, textvariable=c).grid(row=4, column=5)
    self.button = Button(self, text="Browse", command=self.write_to, width=10)
    self.button.grid(row=4, column=10, sticky=W)

    self.button = Button(self, text="Generate CSV", command=self.write_to_csv, width=10)
    self.button.grid(row=8, column=0, sticky=W)

    self.button = Button(self, text="Close", command=self.quit, width=10)
    self.button.grid(row=8, column=10, sticky=W)

  def write_to(self):
    csv_file_path = askdirectory()
    self.output_folder_dest = csv_file_path

  def write_to_csv(self):
    file_to_fetch = self.input_file_path + "\*.xlsx"
    excel_names = (glob.glob(file_to_fetch))

    df = pd.DataFrame()

    for f in excel_names:
        data = pd.read_excel(f)
        df = df.append(data[2:])

    df.to_excel(self.output_folder_dest + "\merged_output.xlsx")
    mbox.showinfo("Information", "CSV Generated!")

  def import_input_csv_data(self):
    csv_file_path = askdirectory()
    self.input_file_path = csv_file_path

if __name__ == "__main__":
    MyFrame().mainloop()

但是当我使用cx_freeze构建可执行文件时,当我必须合并超过7-8个文件时,它不起作用。程序继续运行,内存空间占用超过1GB。

这是setup.py代码:

from cx_Freeze import setup, Executable
import os

base = None
include_files = [r"C:\\Users\\Jio User\\AppData\\Local\\Programs\\Python\\Python36-32\\DLLs\\tcl86t.dll",
                 r"C:\\Users\\Jio User\\AppData\\Local\\Programs\\Python\\Python36-32\\DLLs\\tk86t.dll"]
os.environ['TCL_LIBRARY'] = "C:\\Users\\Jio User\\AppData\\Local\\Programs\\Python\\Python36-32\\tcl\\tcl8.6"
os.environ['TK_LIBRARY'] = "C:\\Users\\Jio User\\AppData\\Local\\Programs\\Python\\Python36-32\\tcl\\tk8.6"

executables = [Executable("combine.py", base=base)]

packages = ["numpy"]
options = {
    'build_exe': {
        'packages':packages,
        'include_files':include_files
    },
}

setup(
    name="xlsx_merger",
    options=options,
    version="0.1",
    description="Badri",
    executables=executables
)

请指出我出错的地方。

谢谢

1 个答案:

答案 0 :(得分:0)

这是我的setup.py文件,该文件适用于tkinter,pandas,matplotlib,seaborn,scipy等。我不得不花很多时间才能使其正常工作。 另一个需要注意的是,如果您正在运行pd.read_excel函数,则必须安装xlrd模块

import sys
from cx_Freeze import setup, Executable
import os

build_exe_options = {"include_files": ["tcl86t.dll", "tk86t.dll"], "packages": ["numpy", "matplotlib", "pygments", "IPython", "pyfolio", "scipy", "empyrical", "seaborn"], 
                     "includes":["matplotlib.backends.backend_tkagg"], "excludes":["scipy.spatial.cKDTree"]} 


base = None  
if sys.platform == "win32":  
    base = "Win32GUI"  


os.environ['TCL_LIBRARY'] = r'C:\Users\lyu\AppData\Local\Programs\Python\Python36\tcl\tcl8.6'
os.environ['TK_LIBRARY'] = r'C:\Users\lyu\AppData\Local\Programs\Python\Python36\tcl\tk8.6'

setup(
      name = "App App",
      version = "1.0",
      description = "Fun for the whole family",
      options = {"build_exe": build_exe_options},
      executables = [Executable("AppApp.py", base = base)],
      package_dir={'': ''},
      )