我的 Python 代码在变成 exe 后无法正常工作

Posted

技术标签:

【中文标题】我的 Python 代码在变成 exe 后无法正常工作【英文标题】:My Python code not working after making it into a exe 【发布时间】:2017-09-21 21:14:36 【问题描述】:

我想编写代码并制作一个 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
)

请指出我哪里出错了。

谢谢

【问题讨论】:

使用 tkinter 制作单个可执行文件,不需要包含所有 tcl/tk 吗? 我的代码适用于 2-3 个文件,但是当我尝试合并包含超过 7-8 个 xlsx 文件的文件夹时它会失败。所以我假设你不必 【参考方案1】:

这是我的 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='': '',
      )

【讨论】:

以上是关于我的 Python 代码在变成 exe 后无法正常工作的主要内容,如果未能解决你的问题,请参考以下文章

使用 PyQt5 制作应用程序后无法打开 .exe

python打包成 exe后程序无法运行?

python spawn cmd脚本并在结束后关闭它

Unity3D中项目在Unity编辑器下正常运行打包成功exe后无法正常运行

Unity3D中项目在Unity编辑器下正常运行打包成功exe后无法正常运行

当我的应用程序无法在调试模式下运行,但 exe 文件可以正常运行时,这意味着啥?