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

2024-01-16 06:03:17 作者:欧亿体育

报错提示

[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 "", line 1030, in _gcd_import
      File "", line 1007, in _find_and_load
      File "", line 972, in _find_and_load_unlocked
      File "", line 228, in _call_with_frames_removed
      File "", line 1030, in _gcd_import
      File "", line 1007, in _find_and_load
      File "", line 986, in _find_and_load_unlocked
      File "", line 680, in _load_unlocked
      File "", line 855, in exec_module
      File "", 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
在线咨询 拨打电话

电话

010-67916526

微信二维码

微信二维码