我正试图在本地机器上运行一个简单的Jupyter笔记本,但奇怪的是我遇到了一些错误.

$ python --version
Python 3.11.5

我只运行了以下命令:

$jupyter notebook

当我 Select 可用的笔记本时,我得到一个500错误,并在控制台中看到以下内容:

ImportError: cannot import name 'contextfilter' from 'jinja2' (/home/joesan/.local/lib/python3.8/site-packages/jinja2/__init__.py)

我试图安装和卸载JJIA2,但都无济于事.我怎么才能解决这个问题呢?

其他信息:

$ jupyter --version
Selected Jupyter core packages...
IPython          : 7.27.0
ipykernel        : 6.4.1
ipywidgets       : 7.6.5
jupyter_client   : 7.0.3
jupyter_core     : 4.8.1
jupyter_server   : not installed
jupyterlab       : not installed
nbclient         : 0.5.4
nbconvert        : not installed
nbformat         : 5.1.3
notebook         : 6.4.4
qtconsole        : 5.1.1
traitlets        : 5.1.0

Opening in existing browser session.
libva error: /usr/lib/x86_64-linux-gnu/dri/i965_drv_video.so init failed
/usr/lib/python3.8/json/encoder.py:257: UserWarning: date_default is deprecated since jupyter_client 7.0.0. Use jupyter_client.jsonutil.json_default.
  return _iterencode(o, 0)
[E 19:20:53.073 NotebookApp] Uncaught exception GET /notebooks/boston_housing.ipynb (127.0.0.1)
    HTTPServerRequest(protocol='http', host='localhost:8888', method='GET', uri='/notebooks/boston_housing.ipynb', version='HTTP/1.1', remote_ip='127.0.0.1')
    Traceback (most recent call last):
      File "/home/joesan/.local/lib/python3.8/site-packages/tornado/web.py", line 1704, in _execute
        result = await result
      File "/home/joesan/.local/lib/python3.8/site-packages/tornado/gen.py", line 775, in run
        yielded = self.gen.send(value)
      File "/home/joesan/.local/lib/python3.8/site-packages/notebook/notebook/handlers.py", line 95, in get
        self.write(self.render_template('notebook.html',
      File "/home/joesan/.local/lib/python3.8/site-packages/notebook/base/handlers.py", line 516, in render_template
        return template.render(**ns)
      File "/home/joesan/.local/lib/python3.8/site-packages/jinja2/environment.py", line 1301, in render
        self.environment.handle_exception()
      File "/home/joesan/.local/lib/python3.8/site-packages/jinja2/environment.py", line 936, in handle_exception
        raise rewrite_traceback_stack(source=source)
      File "/home/joesan/.local/lib/python3.8/site-packages/notebook/templates/notebook.html", line 1, in top-level template code
        {% extends "page.html" %}
      File "/home/joesan/.local/lib/python3.8/site-packages/notebook/templates/page.html", line 154, in top-level template code
        {% block header %}
      File "/home/joesan/.local/lib/python3.8/site-packages/notebook/templates/notebook.html", line 115, in block 'header'
        {% for exporter in get_frontend_exporters() %}
      File "/home/joesan/.local/lib/python3.8/site-packages/notebook/notebook/handlers.py", line 23, in get_frontend_exporters
        from nbconvert.exporters.base import get_export_names, get_exporter
      File "/home/joesan/.local/lib/python3.8/site-packages/nbconvert/__init__.py", line 4, in <module>
        from .exporters import *
      File "/home/joesan/.local/lib/python3.8/site-packages/nbconvert/exporters/__init__.py", line 3, in <module>
        from .html import HTMLExporter
      File "/home/joesan/.local/lib/python3.8/site-packages/nbconvert/exporters/html.py", line 14, in <module>
        from jinja2 import contextfilter
    ImportError: cannot import name 'contextfilter' from 'jinja2' (/home/joesan/.local/lib/python3.8/site-packages/jinja2/__init__.py)

我正在使用pyenv来管理环境:

$ pyenv versions
  system
  3.8.0
* 3.11.5 (set by /home/joesan/.pyenv/version)

推荐答案

请看一下这个错误:

ImportError: cannot import name 'contextfilter' from 'jinja2' (/home/joesan/.local/lib/python3.8/site-packages/jinja2/__init__.py)

它指的是来自Python3.8的jinja2,而您的Python版本是3.11.我猜jupyter没有安装在您的Python3.11环境中,但无论如何它都存在于您的操作系统的PATH中.因为现在我们有了3.8和3.11的奇怪组合,所以您会遇到导入错误.

解决方案:

  1. 确保您有隔离的环境
  2. jupyter安装到您的3.11

Python相关问答推荐

socket.gaierror:[Errno -2]名称或服务未知|Firebase x Raspberry Pi

如何在不使用字符串的情况下将namedtuple属性传递给方法?

如何终止带有队列的Python进程?+ 队列大小的错误?

实现的差异取决于计算出的表达是直接返回还是首先存储在变量中然后返回

Python -Polars库中的滚动索引?

添加包含中具有任何值的其他列的计数的列

根据给定日期的状态过滤查询集

Locust请求中的Python和参数

如何在具有重复数据的pandas中对groupby进行总和,同时保留其他列

重新匹配{ }中包含的文本,其中文本可能包含{{var}

如何使用matplotlib在Python中使用规范化数据和原始t测试值创建组合热图?

Mistral模型为不同的输入文本生成相同的嵌入

如何使用它?

从spaCy的句子中提取日期

Pandas Loc Select 到NaN和值列表

将标签移动到matplotlib饼图中楔形块的开始处

如何在GEKKO中使用复共轭物

如何在Gekko中处理跨矢量优化

有没有办法让Re.Sub报告它所做的每一次替换?

如何使用加速广播主进程张量?