You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

编译Python程序时matplotlib导入_path遇DLL加载失败问题求助

Matplotlib程序PyInstaller打包后运行报错:DLL加载失败

我开发了一款使用matplotlib绘制数据折线图的Python程序,在PyCharm中运行完全正常,但通过PyInstaller或auto-py-to-exe将其编译为exe文件后,运行exe并触发绘图功能时,出现如下错误:

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.

我已经尝试了以下解决方法,但问题仍未解决:

  • 执行pip install msvc-runtime
  • 重装MS Visual C++ Redistributables(x64和x86版本)
  • 确认Windows\System32文件夹中存在matplotlib所需的msvcp140.dll和vcruntime140.dll文件

我无法理解为何程序在IDE中运行正常,编译为exe后却出现该问题,恳请各位提供解决思路。

简化后的核心代码

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()

最新使用的PyInstaller编译命令

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

内容的提问来源于stack exchange,提问作者Jeremy Wilson

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.27 18:23:22