Pyinstaller + Matplotlib -> export to PDF

147 views
Skip to first unread message

Volodymyr

unread,
Aug 26, 2024, 10:40:18 AM8/26/24
to PyInstaller

Dear Pyinstaller and Matplotlib users. 

Found this problem with my project

Matplotlib and Tkinter based application with export to PDF works great until you compile it to Exe file by PyInstaller. So I made simple app that show the problem:

When execute Exe file and try to export pdf file - program drop this error text to console:
ModuleNotFoundError: No module named ‘matplotlib.backends.backend_pdf’
P.S. Importing this “matplotlib.backends.backend_pdf” module in code does not help

My configuration:
Python 3.11.9 x64
Matplotlib 3.9.2
PyInstaller 6.10.0

Command to compile: pyInstaller.exe SaveToPdfApp.py --onedir --noconsole --noconfirm --noupx --console

Simple sample code:


import tkinter as tk
from tkinter import filedialog
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
import matplotlib.pyplot as plt
import numpy as np
#from matplotlib.backends.backend_pdf import PdfPages

def save_plot_as_pdf():
    file_path = filedialog.asksaveasfilename(defaultextension=".pdf", filetypes=[("PDF files", "*.pdf")])
    if file_path:
        #with PdfPages(file_path) as pdf:
            #pdf.savefig(figure)
        figure.savefig(file_path, format="pdf")
        print(f"Plot saved as {file_path}")

root = tk.Tk()
root.title("Matplotlib Plot in Tkinter")

x = np.linspace(0, 10, 100)
y = np.sin(x)

figure = plt.Figure(figsize=(6, 4), dpi=100)
ax = figure.add_subplot(111)
ax.plot(x, y, label="Sine wave")
ax.set_title("Simple Sine Wave Plot")
ax.set_xlabel("X-axis")
ax.set_ylabel("Y-axis")
ax.legend()

canvas = FigureCanvasTkAgg(figure, root)
canvas.get_tk_widget().pack(side=tk.TOP, fill=tk.BOTH, expand=1)

save_button = tk.Button(root, text="Save as PDF", command=save_plot_as_pdf)
save_button.pack(side=tk.BOTTOM)

root.mainloop()

Chris Barker

unread,
Aug 26, 2024, 1:20:12 PM8/26/24
to pyins...@googlegroups.com
This may help:

https://pyinstaller.org/en/stable/hooks-config.html#matplotlib-hooks

However, I'm not sure how the MPL PDF back-end works -- it may be a special case -- depend on ghostscript, or something else that is not pure-python, and may not be getting included. Quick googling didn't help, but something to keep in mind.

HTH,
  -CHB



--
You received this message because you are subscribed to the Google Groups "PyInstaller" group.
To unsubscribe from this group and stop receiving emails from it, send an email to pyinstaller...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/pyinstaller/5df5f033-dada-4b97-849e-397fce45b6e8n%40googlegroups.com.


--

Christopher Barker, Ph.D.
Oceanographer

Emergency Response Division
NOAA/NOS/OR&R            (206) 526-6959   voice
7600 Sand Point Way NE   (206) 526-6329   fax
Seattle, WA  98115       (206) 526-6317   main reception

Chris....@noaa.gov

bwoodsend

unread,
Aug 26, 2024, 1:22:18 PM8/26/24
to PyInstaller

Matplotlib’s array of interchangeable backends model is fairly ambiguous to PyInstaller — particularly if you want more than one backend. Telling PyInstaller which backends you want straightens it out.

# In your spec file a = Analysis( ... hooksconfig={ "matplotlib": { "backends": ["TkAgg", "pdf"], } }, ... )

That said, just adding a dumb --hiddenimport=matplotlib.backends.backend_pdf to the build command does the trick for me too.

Volodymyr

unread,
Aug 28, 2024, 9:22:35 AM8/28/24
to PyInstaller
adding  command line keys--hiddenimport matplotlib.backends.backend_pdf  --hiddenimport matplotlib.backends.backend_svg help me, but with space and not assign(=) symbol

понеділок, 26 серпня 2024 р. о 20:22:18 UTC+3 bwoodsend пише:
Reply all
Reply to author
Forward
0 new messages