使用Pyvis做网络可视化时遇'NoneType'无render属性错误
问题
使用Python 3.9.6版本及最新版Pyvis进行网络可视化,代码如下:
from pyvis.network import Network g = Network() g.add_node(0) g.add_node(1) g.add_edge(0, 1) g.show('test.html')
每次执行g.show()时都会抛出如下错误:
Traceback (most recent call last): File "/Users/tom/Library/Mobile Documents/com~apple~CloudDocs/Projekte/Coding_/f1 standings/test2.py", line 3, in <module> g.show('nx.html') File "/Users/tom/Library/Python/3.9/lib/python/site-packages/pyvis/network.py", line 546, in show self.write_html(name, open_browser=False,notebook=True) File "/Users/tom/Library/Python/3.9/lib/python/site-packages/pyvis/network.py", line 515, in write_html self.html = self.generate_html(notebook=notebook) File "/Users/tom/Library/Python/3.9/lib/python/site-packages/pyvis/network.py", line 479, in generate_html self.html = template.render(height=height, AttributeError: 'NoneType' object has no attribute 'render'
已尝试更新Pyvis、修改代码细节、全量导入pyvis.network,但均无效果。
解决方案
显式关闭notebook模式调用
show
修改show方法参数,强制禁用notebook模式:g.show('test.html', notebook=False)或者直接用
write_html替代show,手动控制浏览器打开:g.write_html('test.html', open_browser=True)修复Pyvis模板文件
该错误本质是Pyvis无法找到Jinja2渲染模板,可能是安装时文件缺失:- 完全卸载后重新安装Pyvis:
pip uninstall -y pyvis pip install pyvis --no-cache-dir - 手动检查Pyvis安装目录下的
templates文件夹(路径为/Users/tom/Library/Python/3.9/lib/python/site-packages/pyvis/templates),若缺失则从Pyvis源码中复制对应模板文件到该目录。
- 完全卸载后重新安装Pyvis:
降级Pyvis版本
最新版Pyvis可能与Python3.9存在兼容性问题,尝试降级到稳定版本:pip install pyvis==0.3.2
内容的提问来源于stack exchange,提问作者Tom Neuburg
相关产品推荐
相关产品推荐

