解决 ImportError: cannot import name ‘contextfilter‘ from ‘jinja2‘

报错提示

[E 17:15:22.581 NotebookApp] Uncaught exception GET /notebooks/%E7%88%AC%E8%99%AB/day02/bs4%E8%A7%A3%E6%9E%90.ipynb (::1)
    HTTPServerRequest(protocol='http', host='localhost:8888', method='GET', uri='/notebooks/%E7%88%AC%E8%99%AB/day02/bs4%E8%A7%A3%E6%9E%90.ipynb', version='HTTP/1.1', remote_ip='::1')
    Traceback (most recent call last):
      File "d:\environment\python\envs\env3.9.5\lib\site-packages\tornado\web.py", line 1704, in _execute
        result = await result
      File "d:\environment\python\envs\env3.9.5\lib\site-packages\tornado\gen.py", line 775, in run
        yielded = self.gen.send(value)
      File "d:\environment\python\envs\env3.9.5\lib\site-packages\notebook\notebook\handlers.py", line 95, in get
        self.write(self.render_template('notebook.html',
      File "d:\environment\python\envs\env3.9.5\lib\site-packages\notebook\base\handlers.py", line 516, in render_template
        return template.render(**ns)
      File "d:\environment\python\envs\env3.9.5\lib\site-packages\jinja2\environment.py", line 1301, in render
        self.environment.handle_exception()
      File "d:\environment\python\envs\env3.9.5\lib\site-packages\jinja2\environment.py", line 936, in handle_exception
        raise rewrite_traceback_stack(source=source)
      File "d:\environment\python\envs\env3.9.5\lib\site-packages\notebook\templates\notebook.html", line 1, in top-level template code
        {% extends "page.html" %}
      File "d:\environment\python\envs\env3.9.5\lib\site-packages\notebook\templates\page.html", line 154, in top-level template code
        {% block header %}
      File "d:\environment\python\envs\env3.9.5\lib\site-packages\notebook\templates\notebook.html", line 115, in block 'header'
        {% for exporter in get_frontend_exporters() %}
      File "d:\environment\python\envs\env3.9.5\lib\site-packages\notebook\notebook\handlers.py", line 40, in get_frontend_exporters
        for name in get_export_names():
      File "d:\environment\python\envs\env3.9.5\lib\site-packages\nbconvert\exporters\base.py", line 141, in get_export_names
        e = get_exporter(exporter_name)(config=config)
      File "d:\environment\python\envs\env3.9.5\lib\site-packages\nbconvert\exporters\base.py", line 101, in get_exporter
        exporter = entrypoints.get_single('nbconvert.exporters', name).load()
      File "d:\environment\python\envs\env3.9.5\lib\site-packages\entrypoints.py", line 82, in load
        mod = import_module(self.module_name)
      File "d:\environment\python\python3.9.5\lib\importlib\__init__.py", line 127, in import_module
        return _bootstrap._gcd_import(name[level:], package, level)
      File "<frozen importlib._bootstrap>", line 1030, in _gcd_import
      File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
      File "<frozen importlib._bootstrap>", line 972, in _find_and_load_unlocked
      File "<frozen importlib._bootstrap>", line 228, in _call_with_frames_removed
      File "<frozen importlib._bootstrap>", line 1030, in _gcd_import
      File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
      File "<frozen importlib._bootstrap>", line 986, in _find_and_load_unlocked
      File "<frozen importlib._bootstrap>", line 680, in _load_unlocked
      File "<frozen importlib._bootstrap_external>", line 855, in exec_module
      File "<frozen importlib._bootstrap>", line 228, in _call_with_frames_removed
      File "d:\environment\python\envs\env3.9.5\lib\site-packages\nbconvert\__init__.py", line 4, in <module>
        from .exporters import *
      File "d:\environment\python\envs\env3.9.5\lib\site-packages\nbconvert\exporters\__init__.py", line 3, in <module>
        from .html import HTMLExporter
      File "d:\environment\python\envs\env3.9.5\lib\site-packages\nbconvert\exporters\html.py", line 14, in <module>
        from jinja2 import contextfilter
    ImportError: cannot import name 'contextfilter' from 'jinja2' (d:\environment\python\envs\env3.9.5\lib\site-packages\jinja2\__init__.py)

解决办法:

升级jupyterhub

pip install --upgrade jupyterhub -i https://pypi.douban.com/simple/
# 虚拟环境去掉--user
pip install --upgrade --user nbconvert -i https://pypi.douban.com/simple/

-i 就是选择镜像源,不然更新很慢。

如果是conda环境,运行以下命令

conda install nbconvert==5.4.1

python镜像源

国内镜像:

https://pypi.douban.com/simple/ 豆瓣
https://mirrors.aliyun.com/pypi/simple/ 阿里
https://pypi.hustunique.com/simple/ 华中理工大学
https://pypi.sdutlinux.org/simple/ 山东理工大学
https://pypi.mirrors.ustc.edu.cn/simple/ 中国科学技术大学
https://pypi.tuna.tsinghua.edu.cn/simple 清华

更新pip

python -m pip install --index-url https://pypi.douban.com/simple --upgrade pip
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值