[Python] Error running matplotlib in a compiled Python program: DLL load failed while...

Discussão em 'Python' iniciado por Stack, Fevereiro 7, 2025.

  1. Stack

    Stack Membro Participativo

    I've got a program where a part of it will plot some data to a simple graph using matplotlib. It works fine when I'm running it from PyCharm, but when I compile it with pyinstaller or auto-py-to-exe, run the program from the exe, and try to get the graph, I get the error:

    ERROR:root:
    Traceback (most recent call last):
    File "weeklyGiving.py", line 837, in graph_by_date
    File "PyInstaller\loader\pyimod03_importers.py", line 495, in exec_module
    File "graphThis.py", line 28, in <module>
    File "PyInstaller\loader\pyimod03_importers.py", line 495, in exec_module
    File "matplotlib\__init__.py", line 153, in <module>
    from . import _api, _version, cbook, _docstring, rcsetup
    File "PyInstaller\loader\pyimod03_importers.py", line 495, in exec_module
    File "matplotlib\rcsetup.py", line 27, in <module>
    from matplotlib.colors import Colormap, is_color_like
    File "PyInstaller\loader\pyimod03_importers.py", line 495, in exec_module
    File "matplotlib\colors.py", line 56, in <module>
    from matplotlib import _api, _cm, cbook, scale
    File "PyInstaller\loader\pyimod03_importers.py", line 495, in exec_module
    File "matplotlib\scale.py", line 22, in <module>
    from matplotlib.ticker import (
    File "PyInstaller\loader\pyimod03_importers.py", line 495, in exec_module
    File "matplotlib\ticker.py", line 138, in <module>
    from matplotlib import transforms as mtransforms
    File "PyInstaller\loader\pyimod03_importers.py", line 495, in exec_module
    File "matplotlib\transforms.py", line 49, in <module>
    from matplotlib._path import (
    ImportError: DLL load failed while importing _path: The specified module could not be found.


    Crawling through various posts about this, I have tried a number of different things, such as running pip install msvc-runtime and reinstalling MS Visual C++ Redistributables (both x64 and x86). The msvcp140.dll and vcruntime140.dll files, which I understand matplotlib requires, both exist in my \Windows\System32 folder.

    I just can't wrap my head around why it works from my IDE, but not after compiling it to exe. Any thoughts?

    My code, simplified, is this:

    import matplotlib.pyplot as plot


    class LineGraph:
    def __init__(self, pairs=[['2023-01-01', 352.76], ['2023-01-15', 725.48]]):
    self.x = []
    self.y = []
    for item in pairs:
    self.x.append(item[0])
    self.y.append(item[1])

    def graph_values_by_date_line(self):
    plot.rc('xtick', labelsize=8)
    plot.rc('ytick', labelsize=8)

    fig, ax = plot.subplots()
    ax.plot(self.x, self.y, linewidth=1.0, marker='o', markersize=2)
    ax.legend()

    plot.xticks(rotation=90)

    figManager = plot.get_current_fig_manager()
    figManager.window.showMaximized()
    plot.show()


    And my most recent command for compiling through pyinstaller is:

    pyinstaller --noconfirm --clean --collect-all matplotlib -i "../resources/icon.ico" --add-data "../resources;resources/" --add-data "../reportlab;reportlab/" --add-data "../gsdll64.dll;gsdll64.dll" --add-data "../gsdll64.lib;gsdll64.lib" --add-data "../gsprint.exe;gsprint.exe" --add-data "../gswin64.exe;gswin64.exe" --add-data "../gswin64c.exe;gswin64c.exe" --distpath "./output" ../WeeklyGiving.py

    Continue reading...

Compartilhe esta Página