When executing exe file: ImportError: matplotlib is required for plotting when the default backend "matplotlib" is selected when executing exe file

When executing exe file: ImportError: matplotlib is required for plotting when the default backend "matplotlib" is selected when executing 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

但是没有解决我的问题

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

这是我脚本中的导入:

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

这是怎么回事,我该如何解决?

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