项目场景:
使用python3.6 使用 pyinstaller 调用 matplotlib 打包成exe时报错:AssertionError: Failed to determine matplotlib's data directory!
问题描述
self._hook_module = importlib_load_source(self.hook_module_name, self.hook_filename)
File “c:\users\71013\appdata\local\programs\python\python36\lib\site-packages\PyInstaller\compat.py”, line 641, in importlib_load_source
return mod_loader.load_module()
File “<frozen importlib._bootstrap_external>”, line 399, in _check_name_wrapper
File “<frozen importlib._bootstrap_external>”, line 823, in load_module
File “<frozen importlib._bootstrap_external>”, line 682, in load_module
File “<frozen importlib._bootstrap>”, line 265, in _load_module_shim
File “<frozen importlib._bootstrap>”, line 684, in _load
File “<frozen importlib._bootstrap>”, line 665, in _load_unlocked
File “<frozen importlib._bootstrap_external>”, line 678, in exec_module
File “<frozen importlib._bootstrap>”, line 219, in _call_with_ames_removed
File “c:\users\71013\appdata\local\programs\python\python36\lib\site-packages\PyInstaller\hooks\hook-matplotlib.py”, line 15, in
assert mpl_data_dir, “Failed to determine matplotlib’s data directory!”
AssertionError: Failed to determine matplotlib’s data directory!
原因分析:
这是由pyinstaller 的 matplotlib钩子引起的错误,由于某种原因,应该获取数据路径的exec_statement()函数不起作用。(这是python的一个bug)
解决方案:
首先,找到python的安装路径中,你的三方库目录(site-packages
)中,找到PyInstaller
的文件夹,进入到hooks
文件夹中,找到hook-matplotlib.py
文件。
提示路径可能如下: C:\Users\xxxx\AppData\Local\Programs\Python\Python36\Lib\site-packages\PyInstaller\hooks
其次,打开hook-matplotlib.py文件,注释掉原来的代码,输入下方代码,点击保存,再重新执行打包动作。
新代码如下:
import matplotlib mpl_data_dir = matplotlib.get_data_path() datas = [ (mpl_data_dir, "matplotlib/mpl-data"), ]
结果展示: