【问题标题】:Matplotlib directory not found while using Pyinstaller to create exe from py files使用 Pyinstaller 从 py 文件创建 exe 时找不到 Matplotlib 目录
【发布时间】:2021-07-24 11:16:12
【问题描述】:

我一直在尝试从我的 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 的数据目录!"

AssertionError: 无法确定 matplotlib 的数据目录!

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

【问题讨论】:

  • pyinstaller 很难创建 exe,而且很多时候你需要打包它。您可以分享可以重现您的错误的最少代码吗?那么我们可以尝试为您提供建议。
  • 我推荐使用Anaconda Distribution,它可能会让你的生活更轻松。 Package List

标签: python matplotlib pyinstaller exe


【解决方案1】:

感谢 wedesoft:

pip uninstall pathlib

完成了这项工作。我用os.path替换了pathlib代码;一切都很完美。

注意:更新matplotlibpyinstaller 对我没有帮助。我确信它会在较新的 pyinstaller 版本中得到修复。更多关于相同问题的信息:

https://github.com/pyinstaller/pyinstaller/issues/5004

【讨论】:

  • 这解决了我的问题。我正在运行 python 3.8.10,matplotlib 3.4.2
  • 谢谢,我遇到了同样的问题并尝试了很多东西,现在找到了,它可以正常工作^^
【解决方案2】:

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

【讨论】:

  • 如果有人需要包含它怎么办?
  • matplotlib 删除了数据目录的默认变量,因此找不到它。但是,它只是在 matplotlib v3.2.2 之后才被删除,因此为什么降级到以下任何内容都可以解决此问题并解决您的问题
【解决方案3】:

这是由 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"), 
]

【讨论】:

  • 请不要添加代码图片。您可以将代码添加为文本。参考How do I format my posts using Markdown or HTML?
  • 我不知道可视化编辑器,谢谢!
  • 我无法在我的系统中找到 pyinstaller 所在的 hooks 文件夹。
【解决方案4】:

我尝试按照其他人的建议更改 hook-matplotlib.py 文件,但事实证明最新版本的 Pyinstaller 已经有了这些更改,所以只要你的 pyinstaller 更新了,你在这方面应该没问题(通过运行 @ 检查987654322@)

但是,我仍然遇到“无法确定 matplotlib 的数据目录!”问题并能够通过以下方式解决:

  1. 正在运行pip uninstall pathlib

这很奇怪,但是(不幸的是)Python 世界中有两个 pathlib 包:一个是内置的,另一个是 PyPi 上的。如果您偶然使用 PyPi,hook-matplotlib.py 中的 get_data_path() 函数将无法按预期工作,并将导致该断言失败

  1. 在 Pyinstaller 命令中,包含 --hidden-import matplotlib

不确定这是否是 100% 必要的,但我将它包含在我的 pyinstaller 命令中以确保使用 matplotlib 挂钩文件(例如:pyinstaller --onefile --windowed --hidden-import "matplotlib" ...

希望这会有所帮助,这个决议真的很令人沮丧

【讨论】:

  • 我不得不说我很幸运能在上面看到你的建议。节省了我很多时间和挫折!谢谢。
【解决方案5】:

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

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

【讨论】:

  • 这对我来说似乎克服了这个错误。
【解决方案6】:

我的错误(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 的一部分,因此无需安装旧模块)。

【讨论】:

    【解决方案7】:

    我在尝试创建裸可执行文件时遇到了这个问题我卸载了 matplotlib 并且 PyInstaller 试图打包不存在的模块。

    pip 卸载并没有从站点包中完全删除 matplotlib 的目录,在手动删除它之后(对于 PyQt5 也是如此),exe 按预期构建。

    【讨论】:

      猜你喜欢
      • 2022-11-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-08-12
      • 1970-01-01
      • 2018-06-08
      • 2016-02-12
      相关资源
      最近更新 更多