使用 Pyinstaller 从 py 文件创建 exe 时找不到 Matplotlib 目录

时间:2021-05-01 10:39:22

标签: python matplotlib pyinstaller exe

我一直在尝试从我的 py 文件创建一个 exe 文件。有多个 py 文件,但是,1 个入口点文件。我的代码从 html、csv、xml 文件中获取输入并生成一个 word 文件作为输出。

我使用的是 Python 3.9,尝试使用 Pyinstaller 4.2、5(dev)。两者都给出相同的错误。如果我尝试转换没有 matplotlib 的文件,则转换成功。我也尝试过不同版本的 matplotlib。具体来说,4.3.1、4.3.0rc1、3.2.2。但是,每次我都遇到同样的错误。

<块引用>

assert mpl_data_dir, "无法确定 matplotlib 的数据目录!"

断言错误:无法确定 matplotlib 的数据目录!

根据其他人遇到的类似问题,我也尝试对钩子文件进行更改,但仍然存在相同的问题。

4 个答案:

答案 0 :(得分:0)

我不明白为什么,但是当我们安装 matplotlib==3.0.2 和 pyinstaller==4.2 时问题就解决了

答案 1 :(得分:0)

您可以坚持使用当前的 matplotlib 版本,但必须更新 pyinstaller (5.0.dev0):

pip install -U https://github.com/pyinstaller/pyinstaller/archive/develop.zip

答案 2 :(得分:0)

这是由 pyinstaller matplotlib 钩子引起的错误,出于某种原因,本应获取数据路径的 exec_statement() 函数不起作用。这对我有用:

  1. 转到安装 pyinstaller 的文件夹。
  2. 转到 hooks 文件夹。
  3. 找到并打开 hook-matplotlib.py 文件。
  4. 删除 PyInstaller 导入,然后导入 matplotlib。
  5. 将exec_statement()函数改成matplotlib.get_data_path()函数,就可以删除assert了。

如果你的操作正确,你的代码应该是这样的:

import matplotlib

mpl_data_dir = matplotlib.get_data_path()
datas = [ 
    (mpl_data_dir, "matplotlib/mpl-data"), 
]

答案 3 :(得分:0)

在我的案例中的错误(Python 3.8、PyInstaller 4.3、matplotlib 3.3.3)是这样的:

 ........
 File "c:\users\dev\appdata\local\programs\python\python38\lib\ntpath.py", line 293, in expanduser
    path = os.fspath(path)
TypeError: expected str, bytes or os.PathLike object, not WindowsPath
Traceback (most recent call last):
    ........
    assert mpl_data_dir, "Failed to determine matplotlib's data directory!"

我依赖于 pathlib,它破坏了 matplotlib 的 PyInstaller 钩子。从 setup.py/requirements.txt 中删除 pathlib 并卸载 pathlib 后,它起作用了(pathlib 是 Python 3.8 的一部分,因此无需安装旧模块)。