最近使用Python写了一个可视化程序,中间使用到pyecharts库,单独运行没有问题,但在使用pyinstall打包成一个单独的exe文件后,运行程序报错:
jinja2.exceptions.TemplateNotFound: chart_component.html
网上查找相关资料后,说是因为pyinstaller和pyecharts不兼容导致,尝试后解决方法如下:
1. 更改打包方式,使用-D参数打包成文件件,而不是使用-F参数直接打包为一个exe文件
如 pyinstaller -D -p d:\path run.py
-p d:\path是你的文件中导入包的site-packages路径,假如pyecharts库在电脑上路径是C:\Python37\Lib\site-packages,这里就写成-p C:\Python37\Lib\site-packages
run.py是需要打包的目标入口文件
2. pyinstaller打包完成后会生成build和dist两个文件夹,打开dist文件夹将C:\Python37\Lib\site-packages下的pyecharts文件夹拷贝到该目录下
运行打包后的exe程序,报错消失
FileNotFoundError: [Errno 2] No such file or directory: ‘C:\Users\xxx\AppData\Local\Temp\_MEI944442\pyecharts\datasets\map_filename.json’python
使用上面的方式打包的程序不是单独文件,查阅资料是因为pyinstall和pyecharts不兼容所致,通过以下方法可以使用-F 参数直接打包成一个exe程序
在PyInstaller安装目录下增加一个文件
- C:\Python37\site-packages\PyInstaller\hooks\ 须要增加以下文件
hook-pyecharts.py 文件内容:
#----------------------------------------------------------------------------- # Copyright (c) 2017-2020, PyInstaller Development Team. # # Distributed under the terms of the GNU General Public License (version 2 # or later) with exception for distributing the bootloader. # # The full license is in the file COPYING.txt, distributed with this software. # # SPDX-License-Identifier: (GPL-2.0-or-later WITH Bootloader-exception) #----------------------------------------------------------------------------- # Hook for nanite: https://pypi.python.org/pypi/nanite from PyInstaller.utils.hooks import collect_data_files datas = collect_data_files('pyecharts')
这样就可以使用pyinstall -i titie.ico -w -F xxx.py打包成一个文件了