pyinstall打包python程序出现报错:jinja2.exceptions.TemplateNotFound: chart_component.html的解决办法

最近使用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打包成一个文件了

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值