Jupyter笔记本发布错误

时间:2018-05-04 07:51:55

标签: python jupyter-notebook

当我尝试运行命令jupyter-notebook时,出现以下错误:

`Traceback (most recent call last):
  File "/home/leo/anaconda2/bin/jupyter-notebook", line 7, in <module>
    from notebook.notebookapp import main
  File "/usr/lib/python3/dist-packages/notebook/notebookapp.py", line 31, in <module>
    from zmq.eventloop import ioloop
  File "/usr/lib/python3/dist-packages/zmq/__init__.py", line 34, in <module>
    from zmq import backend
  File "/usr/lib/python3/dist-packages/zmq/backend/__init__.py", line 40, in <module>
    reraise(*exc_info)
  File "/usr/lib/python3/dist-packages/zmq/backend/__init__.py", line 27, in <module>
    _ns = select_backend(first)
  File "/usr/lib/python3/dist-packages/zmq/backend/select.py", line 26, in select_backend
    mod = __import__(name, fromlist=public_api)
  File "/usr/lib/python3/dist-packages/zmq/backend/cython/__init__.py", line 6, in <module>
    from . import (constants, error, message, context,
ImportError: cannot import name constants

我遇到的许多解决方案都需要卸载并重新安装pyzmq软件包(通过pip,conda,apt install;我已经尝试了所有这些)。但这对我没有用。

但是,看看我得到的错误,我认为第二行出现了问题,恰恰在这里:"/usr/lib/python3/dist-packages/notebook/notebookapp.py"。看起来我的Python的两个版本之间存在一些冲突。

我的PC上安装了Anaconda for Python 2.7,但最近我不得不为Python 3.x安装Spyder。可能是安装后出现问题的情况吗?

我已尝试过的内容:

  • 卸载/重新安装pyzmq
  • 卸载/重新安装jupyter
  • 尝试运行jupyter kernelspec list,但遇到了类似的错误(导致我认为其实是冲突):

    Traceback (most recent call last): File "/home/leo/anaconda2/bin/jupyter-kernelspec", line 7, in <module> from jupyter_client.kernelspecapp import KernelSpecApp File "/usr/lib/python3/dist-packages/jupyter_client/__init__.py", line 4, in <module> from .connect import * File "/usr/lib/python3/dist-packages/jupyter_client/connect.py", line 22, in <module> import zmq File "/usr/lib/python3/dist-packages/zmq/__init__.py", line 34, in <module> from zmq import backend File "/usr/lib/python3/dist-packages/zmq/backend/__init__.py", line 40, in <module> reraise(*exc_info) File "/usr/lib/python3/dist-packages/zmq/backend/__init__.py", line 27, in <module> _ns = select_backend(first) File "/usr/lib/python3/dist-packages/zmq/backend/select.py", line 26, in select_backend mod = __import__(name, fromlist=public_api) File "/usr/lib/python3/dist-packages/zmq/backend/cython/__init__.py", line 6, in <module> from . import (constants, error, message, context, ImportError: cannot import name constants

修改

我已经运行了命令

pip3 uninstall jupyter

作为 Sangram Gaikwad 建议,所以现在我不再使用错误的jupyter笔记本了。但是,如果我尝试执行jupyter-notebook,现在又出现了另一个错误:

Traceback (most recent call last):
  File "/home/leo/anaconda2/bin/jupyter-notebook", line 7, in <module>
    from notebook.notebookapp import main
  File "/home/leo/anaconda2/lib/python2.7/site-packages/notebook/__init__.py", line 25, in <module>
    from .nbextensions import install_nbextension
  File "/home/leo/anaconda2/lib/python2.7/site-packages/notebook/nbextensions.py", line 27, in <module>
    from jupyter_core.utils import ensure_dir_exists
ImportError: cannot import name ensure_dir_exists

我尝试了什么:

  • conda update jupyter_core jupyter_client(表示他们已经安装了)

  • conda uninstall jupyter notebook,然后是conda install jupyter notebook(仍然会收到同样的错误)

3 个答案:

答案 0 :(得分:1)

似乎你的anaconda环境中没有更新/安装jupyter notebook和pyzmq软件包。

错误堆栈显示,您正在使用安装在根python3中的jupyter笔记本(一个存在于/usr/bin/python3中)而不是anaconda python3(存在于/home/leo/anaconda2/bin中)。

从追溯堆栈中可以看出 /usr/lib/python3/dist-packages/jupyter_client/__init__.py /usr/lib/python3/dist-packages/zmq/__init__.py

我的解决方案:

  • 尝试使用python3。
  • 删除已安装的anaconda2(sudo rm -rf /home/leo/anaconda2
  • 卸载所有现有的jupyter笔记本,jupyter_client和pyzmq (sudo pip unistall notebook jupyter_client pyzmq
  • 安装anaconda3(使用python3),然后使用conda安装jupyter notebook。 (conda install notebook

答案 1 :(得分:1)

我遇到了同样的问题。许多文章建议在以下两个命令下运行以对其进行修复:
pip3 uninstall pyzmq

pip3 install pyzmq

但是,真正与我合作的是运行此命令:

pip3 install --upgrade pyzmq jupyterlab jupyter --force-reinstall

答案 2 :(得分:0)

从您的个人资料中我发现您是Ubuntu用户。可以从Ubuntu 17.04及更高版本的默认Ubuntu存储库安装 jupyter-notebook 包。如果你使用Ubuntu 17.10或Ubuntu 18.04 LTS,这个答案会很好。如果您还没有使用Ubuntu 18.04,我认为在Ubuntu 18.04中安装Anaconda没有多大优势。

要在Ubuntu 17.04和17.10中安装Jupyter Notebook,请打开终端并输入:

sudo apt install jupyter-notebook jupyter-core python-ipykernel 

要在Ubuntu 18.04及更高版本中安装Jupyter Notebook,请打开终端并输入:

sudo apt install python3-notebook jupyter-core python-ipykernel

python-ipykernel是在Jupyter Notebook中运行Python 2.x程序所必需的,否则它只支持Python 3.x.

要启动笔记本服务器,请运行以下命令:

jupyter notebook

您应该会在网络浏览器中看到Jupyter Notebook。