由于 nest_asyncio.py,无法 运行 Jupyter 笔记本

Cannot run Jupyter notebooks due to nest_asyncio.py

我在 Mac 上使用 Python 3.9.9 通过 pyenv 和 Intel 芯片。我使用 pip install juypter 安装了 Jupyter。当我 运行 which jupyter 我得到响应 /Users/<username>/.pyenv/shims/jupyter,所以 Jupyter 已经安装。但是,当我 运行 jupyter notebook 时出现以下错误:

Traceback (most recent call last):
  File "/Users/<username>/.pyenv/versions/3.9.9/bin/jupyter-notebook", line 8, in <module>
    sys.exit(main())
  File "/Users/<username>/.pyenv/versions/3.9.9/lib/python3.9/site-packages/jupyter_core/application.py", line 264, in launch_instance
    return super(JupyterApp, cls).launch_instance(argv=argv, **kwargs)
  File "/Users/<username>/.pyenv/versions/3.9.9/lib/python3.9/site-packages/traitlets/config/application.py", line 845, in launch_instance
    app.initialize(argv)
  File "/Users/<username>/.pyenv/versions/3.9.9/lib/python3.9/site-packages/traitlets/config/application.py", line 88, in inner
    return method(app, *args, **kwargs)
  File "/Users/<username>/.pyenv/versions/3.9.9/lib/python3.9/site-packages/notebook/notebookapp.py", line 2141, in initialize
    self._init_asyncio_patch()
  File "/Users/<username>/.pyenv/versions/3.9.9/lib/python3.9/site-packages/notebook/notebookapp.py", line 2117, in _init_asyncio_patch
    nest_asyncio.apply()
  File "/Users/<username>/.pyenv/versions/3.9.9/lib/python3.9/site-packages/nest_asyncio.py", line 14, in apply
    raise ValueError('Can\'t patch loop of type %s' % type(loop))
ValueError: Can't patch loop of type <class 'NoneType'>

我以前从未见过这个错误,我寻找解决方案也没有结果。有什么想法吗?

遇到类似问题并使用以下方法解决:

pip3 install --upgrade pip 
pip3 install jupyter

也许在你的情况下你应该使用 pip3.9

你可以检查 pip 和 python 与

的匹配
python --version
python -m pip --version

发现这是 jupyter 和 python 3.9 的问题:

https://github.com/jupyter/notebook/issues/5912

一个os可行的解决方案是:

    • conda create -n py39 python=3.9
    • pip3 安装 --upgrade pip
    • pip3 安装 jupyter
    • 源码激活py39
    • 运行 jupyter-notebook

另外,本期包含macos具体解决方案:

https://github.com/jupyter/notebook/issues/5872

显然这个错误发生在 nest-asyncio=1.5.2 (issue)。升级到 nest-asyncio=1.5.3 对我有用。您可以运行以下命令卸载现有版本并升级到1.5.3:

pip uninstall nest-asyncio
pip install nest-asyncio==1.5.3