Search code examples
pythonmatplotlibpyinstaller

Problem with backend matplotlib when exporting program with pyinstaller


My code is plotting graph from excel documents (or csv) and then saving them automatically. It uses tkinter as an user interface.

I have a problem with matplotlib backend when trying to export my program with pyinstaller : I know this is a known issue. I have been trying to resolve it reading other comments, help on github : Here

But I am a beginner and can't understand it.


I tried several things :

1 - Deleting and re-installing matplotlib, updating it

2 - in my code, using the command import matplotlib.backends.backend_tkagg


I found some comments about solutions like this :

A -

try:
    # PyInstaller creates a temp folder and stores path in _MEIPASS
    base_path = sys._MEIPASS
except AttributeError:
    # If not running from a PyInstaller bundle, use the current working directory
    base_path = os.path.abspath(".")

# Append the filename (e.g., "ffmpeg.exe") to the base path
ffmpeg_path = os.path.join(base_path, "ffmpeg.exe")

B -

import nltk                                              # hook for nltk
from PyInstaller.utils.hooks import collect_data_files   # add datas for nltk

datas = collect_data_files('nltk', False)

for p in nltk.data.path:                                 # loop through the data directories and add them
    if os.path.exists(p):
        datas.append((p, "nltk_data"))

But I don't know what it does and so can't really link this to my code, I tried to put this just raw at the beginning but it did not solve my issue.


The error message I get is

ModuleNotFoundError: No module named "matplotlib.backends.backend_svg"
Failed to execute script "myscript.py" due to unhandled exeption!

Note that this is only at the end of the program that this message appear, all the other parts (besides saving pictures) work.


Edit : With further Trials and errors, I now see that the problem is due to the svg format. Maybe it is not possible to use the "classic" backend with svg files.


Solution

  • Ok I found my error, this is actually really simple when you think about it :

    import matplotlib.backends.backend_svg
    

    I did not know this worked like that