2

When I try to run the command jupyter-notebook, I get the following error:

`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

Many of the solutions I've encountered required to uninstall and reinstall the pyzmq package (via pip, conda, apt install; I've tried all of them). But that didn't work for me.

However, looking at the error I get, I think there's something wrong in the second line, precisely here: "/usr/lib/python3/dist-packages/notebook/notebookapp.py". It looks like there's some conflict among the two versions of Python I have.

I have Anaconda for Python 2.7 installed in my PC, but recently I had to install Spyder for Python 3.x. May it be the case that something got corrupted after the installation?

What I already tried:

  • uninstall/reinstall pyzmq
  • uninstall/reinstall jupyter
  • tried to run jupyter kernelspec list, but got a similar error (which leads me to think there's actually a conflict):

    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


EDIT:

I've run the command

pip3 uninstall jupyter

as Sangram Gaikwad suggested, so now I'm not using the wrong jupyter notebook anymore. However, if I try to execute jupyter-notebook, now I get another error:

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

What I tried:

  • conda update jupyter_core jupyter_client (it says they're already installed)

  • conda uninstall jupyter notebook, then conda install jupyter notebook (still get the same error)

4 Answers 4

6

I have faced same issue. Many posts suggest running below two commands to fix it:
pip3 uninstall pyzmq

pip3 install pyzmq

But, what really worked with me was running this command :

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

0
1

It seems that jupyter notebook and pyzmq package are not updated/installed in your anaconda environment.

Error stack shows that you are using jupyter notebook installed in your root python3 (one which is present in /usr/bin/python3) instead of anaconda python3 (present in /home/leo/anaconda2/bin).

As evident from traceback stack /usr/lib/python3/dist-packages/jupyter_client/__init__.py /usr/lib/python3/dist-packages/zmq/__init__.py

My solution:

  • Try using python3.
  • Remove anaconda2 that you have installed (sudo rm -rf /home/leo/anaconda2)
  • Uninstall all existing jupyter notebook, jupyter_client and pyzmq (sudo pip uninstall notebook jupyter_client pyzmq)
  • Install anaconda3 (with python3) and then use conda to install jupyter notebook. (conda install notebook)
3
  • Thanks for the tip, now I get another error. I edited the question
    – leqo
    Commented May 4, 2018 at 13:18
  • @leqo Please use anaconda3 (with python3). Latest jupyter notebook versions do not support python2 (anaconda2). Commented May 4, 2018 at 14:32
  • Well, a few days ago everything was fine (I had Jupyter notebook with both the Python 2 and Python 3 core), so I think there must be a solution
    – leqo
    Commented May 5, 2018 at 9:07
0

Had to go through several upgrades in the command line to solve dependencies

pip install --upgrade "package name"

Mine included:

  • nbconvert
  • nbformat
  • nbinteract
  • nbclient
  • jupyter
  • traitlets
  • Jinja
0

From your profile I see that you are an Ubuntu user. The jupyter-notebook package can be installed from the default Ubuntu repositories in Ubuntu 17.04 and later. This answer would be good to try if you are using either Ubuntu 17.10 or Ubuntu 18.04 LTS. If you're not already using Ubuntu 18.04 I don't think there's much of an advantage in installing Anaconda in Ubuntu 18.04.

To install Jupyter Notebook in Ubuntu 20.04 and later open the terminal and type:

sudo apt install jupyter-notebook

To install Jupyter Notebook in Ubuntu 18.04-19.10 open the terminal and type:

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

python-ipykernel is necessary for running Python 2.x programs in Jupyter Notebook, which otherwise supports only Python 3.x.

To start the notebook server run the following command:

jupyter notebook

You should see Jupyter Notebook open in your web browser.

To install Jupyter Notebook in Ubuntu 17.04 and 17.10 open the terminal and type:

sudo apt install jupyter-notebook jupyter-core python-ipykernel 
2
  • I tried your suggestions, but it didn't work. After executing pip3 uninstall jupyter, I get a different error (I edited the questions btw)
    – leqo
    Commented May 4, 2018 at 13:19
  • That's why I told you that Anaconda isn't worthwhile in Ubuntu. Almost all the same packages from Anaconda are in the 18.04 default repos. Anaconda is known for being unable to find the paths to any package that conda didn't install, so you have to add the path to each Python executable manually or else Anaconda won't be able to find it. Anaconda is selling you the idea that it's easier to use than the Ubuntu default repos, but in the long run it's much harder to use Anaconda in Ubuntu.
    – karel
    Commented May 4, 2018 at 14:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.