首页 > 解决方案 > 执行 exe 文件时:ImportError: matplotlib is required for plotting when the default backend "matplotlib" is selected when execution exe file

问题描述

我使用 pyinstaller 创建了一个 .exe 文件。当我在 Pycharm 上运行它时,它运行良好。但是当我尝试运行可执行文件时会发生错误。

这是完整的错误:

Traceback (most recent call last):
  File "DT_070621.py", line 397, in <module>
  File "DT_070621.py", line 315, in arquivos_saida
  File "pandas\plotting\_core.py", line 892, in __call__
  File "pandas\plotting\_core.py", line 1814, in _get_plot_backend
  File "pandas\plotting\_core.py", line 1754, in _load_backend
ImportError: matplotlib is required for plotting when the default backend "matplotlib" is selected.
[11304] Failed to execute script 'DT_070621' due to unhandled exception!

但是当我检查 matplotlib 版本时,一切似乎都很好。

>>> import matplotlib
>>> matplotlib.__version__
'3.4.3'

我还检查了正在使用的后端:

>>> import matplotlib.pyplot as plt
>>> plt.get_backend()
'TkAgg'

我也尝试过像这样创建 .exe 文件:

pyinstaller --hidden-import=openpyxl --hidden-import=matplotlib --onefile DT_070621.py

但这并没有解决我的问题。

我也检查了这个问题,但它的描述似乎不完整,并且没有对帮助我解决这个问题有用的答案。

这是我脚本中的导入:

import xml.etree.ElementTree as ET
from xml.etree import ElementTree
import pandas as pd
from shapely.geometry import Point, Polygon
import numpy as np
import matplotlib.pyplot as plt
import os

发生了什么事,我该如何解决?

标签: pythonmatplotlibpyinstaller

解决方案


添加--hidden-import pandas.plotting._matplotlib解决了它。


推荐阅读