我有一个基于 tkinter 的 GUI,它允许用户将 csv 文件作为数据框加载并将其绘制在图形画布上。从那里我希望使用 pyinstaller 创建一个 Mac 应用程序。我在运行 Big Sur 的 Mac 上使用 Python 3.9。首先,我的主脚本,标题为“Read_CSV_Plot.py”:
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
from matplotlib import pyplot as plt
import numpy as np
import pandas as pd
from matplotlib.figure import Figure
from tkinter import Tk,filedialog,TOP,Menu
# Main Window
root = Tk()
# File selection
def _open():
root.filename = filedialog.askopenfilename(initialdir=
"Users/fishbacp/Desktop",title ="Select file",filetypes = (("CSV files","*.csv"), ("all files","*.*")))
df=pd.read_csv(root.filename)
fig = plt.figure()
ax = fig.add_subplot(111)
df.plot( ax=ax)
canvas = FigureCanvasTkAgg(fig, master=root)
canvas.draw()
canvas.get_tk_widget().pack(side=TOP)
# Quit function
def _quit():
root.quit()
root.destroy()
# Main pull-down menu options to open file and exit.
menubar = Menu(root)
filemenu = Menu(menubar, tearoff=0)
menubar.add_cascade(label="File", menu=filemenu)
filemenu.add_command(label="Open", command=_open)
filemenu.add_command(label="Exit", command=_quit)
root.config(menu=menubar)
root.mainloop()
为了创建应用程序,我尝试按照 https://pyinstaller.readthedocs.io/en/stable/spec-files.html#spec-file-options-for-a-mac-os-x-bundle 上的 pyinstaller 说明进行操作。我知道我需要添加一个 Bundle 选项,并且我添加了一些隐藏的导入:
block_cipher = None
a = Analysis(['Read_CSV_Plot.py'],
pathex=['/Users/fishbacp/Desktop/Python_May_2021'],
hiddenimports=['pandas','numpy','matplotlib','tkinter',
'matplotlib.backends.backend_tkagg.FigureCanvasTkAgg']
binaries=[],
datas=[],
hookspath=[],
runtime_hooks=[],
excludes=[],
win_no_prefer_redirects=False,
win_private_assemblies=False,
cipher=block_cipher,
noarchive=False)
pyz = PYZ(a.pure, a.zipped_data,
cipher=block_cipher)
exe = EXE(pyz,
a.scripts,
a.binaries,
a.zipfiles,
a.datas,
[],
name='Read_CSV_Plot',
debug=False,
bootloader_ignore_signals=False,
strip=False,
upx=True,
upx_exclude=[],
runtime_tmpdir=None,
console=True )
app = BUNDLE(exe,name='Read_CSV_Plot.app',icon=None, bundle_identifier=None)
在我第一次进入的终端
pyi-makespec --onefile —-windowed Read_CSV_Plot.py
关注
pyinstaller Read_CSV_Barplot.spec Read_CSV_Plot.py
一个应用程序出现在 dist 文件夹中,我试图在终端上运行它。但是,这样做会产生以下错误消息:
Traceback (most recent call last):
File "Read_CSV_Plot.py", line 1, in <module>
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
ModuleNotFoundError: No module named 'matplotlib'
我不明白这个错误的原因,因为我添加了 matplotlib 和 matplotlib.backends.backend_tkagg.FigureCanvasTkAgg 作为隐藏的导入。
答案 0 :(得分:0)
试试这个 找到 hook-matplotlib.py 并将其更改为
[markup.goldmark.renderer]
unsafe = true
然后用 pyinstaller 重试