在编译的Python程序中运行matplotlib时出错:导入路径时DLL加载失败

kmb7vmvb  于 2023-03-19  发布在  Python
关注(0)|答案(1)|浏览(330)

我有一个程序,它的一部分使用matplotlib将一些数据绘制成一个简单的图形,当我从PyCharm运行它时,它工作得很好,但是当我用pyinstaller或auto-py-to-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)。msvcp140.dll和vcruntime140.dll文件(我知道matplotlib需要它们)都存在于我的\Windows\System32文件夹中。
我只是不明白为什么它能在我的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
ozxc1zmp

ozxc1zmp1#

原来我在某个时候安装了一个开发版本的matplotlib,我用pip install matplotlib==3.7.0回滚了它,它工作得很好。

相关问题