'Jupyter Notebook - Cannot Connect to Kernel

I've tried installing jupyter notebook using pip3 install jupyter. Every time i launch a new jupyter notebook, the notebook is unable to connect to the kernel. See screenshot below Attaching my requirements.txt file, also i'm on Python version 3.6.8. Jupyter version is 4.4.0 with notebook version 5.7.4.

Where did i go wrong here and how can i fix this? Thanks!

attrs==18.2.0
backcall==0.1.0
bleach==3.1.0
colorama==0.4.1
cycler==0.10.0
decorator==4.3.2
defusedxml==0.5.0
entrypoints==0.3
ipykernel==5.1.0
ipython==7.3.0
ipython-genutils==0.2.0
ipywidgets==7.4.2
jedi==0.13.3
Jinja2==2.10
jsonschema==3.0.1
jupyter==1.0.0
jupyter-client==5.2.4
jupyter-console==6.0.0
jupyter-core==4.4.0
kiwisolver==1.0.1
MarkupSafe==1.1.1
matplotlib==3.0.3
mistune==0.8.4
nbconvert==5.4.1
nbformat==4.4.0
notebook==5.7.4
numpy==1.16.2
pandas==0.24.1
pandocfilters==1.4.2
parso==0.3.4
pickleshare==0.7.5
prometheus-client==0.6.0
prompt-toolkit==2.0.9
Pygments==2.3.1
pyparsing==2.3.1
pyrsistent==0.14.11
python-dateutil==2.8.0
pytz==2018.9
pywinpty==0.5.5
pyzmq==18.0.0
qtconsole==4.4.3
scikit-learn==0.20.3
scipy==1.2.1
seaborn==0.9.0
Send2Trash==1.5.0
six==1.12.0
sklearn==0.0
terminado==0.8.1
testpath==0.4.2
tornado==6.0
traitlets==4.3.2
wcwidth==0.1.7
webencodings==0.5.1
widgetsnbextension==3.4.2

Example

enter image description here



Solution 1:[1]

There is a likelihood that you've updated Tornado to 6.0.0 recently. Try reinstalling it to version 5.1.1. It just helped me. Lost 1 hour of precious Saturday time on this.

Solution 2:[2]

Create a conda environment using:

conda create -n my_env python=3

Activate the environment using:

source activate my_env

Install the ipykernel using:

conda install ipykernel

ipython kernel install --name my_env --user

Now open the jupyter and select the "my_env" from Kernel option. ( path : jupyter -> kernel -> change kernel -> my_env )

Solution 3:[3]

In my case, I just updated my jupyter and it worked.

Solution 4:[4]

If the above does not work for you then this is highly recommended.

I had the same issue where the kernel would simply not connect. Tried every solution, still no connection.

In the end I noticed some Value Error Exceptions being raised in the console where I started the notebook client. The source of these exceptions was another python script in the same folder as the notebook.

delete all the python files in the same directory where notebook is installed or move them to some other folder. Fixed the notebook

Solution 5:[5]

Just update the notebook, should work

Solution 6:[6]

That worked for me under Python 3.7.

pip3 install tornado==5.1.1
pip3 install notebook==5.7.4

Solution 7:[7]

I got this error:

from prompt_toolkit.eventloop import generator_to_async_generator
ImportError: cannot import name 'generator_to_async_generator' from 'prompt_toolkit.eventloop' (/Users/danielmlow/miniconda3/envs/pydra/lib/python3.7/site-packages/prompt_toolkit/eventloop/__init__.py)

So I upgraded prompt_toolkit:

pip install prompt_toolkit==3.0.

Solution 8:[8]

My approach is similar to @AzizNal and @delx's answer.

I tracked my particular manifestation of this problem to the following:

  • I had a bunch of miscellaneous statements that I wanted to keep, and saved these in a file ./myworkingdirectory/random.py.

  • Any notebooks I attempted to open in the same directory as where this file was had trouble connecting to the kernel.

    (Likewise, any Spyder environment opened with a working direction of myworkingdirectory was crashing out and couldn't connect to a kernel)

  • Closer inspection of the error told me that numpy: cannot import name SystemRandom.

Given the above, I think this random.py of mine was conflicting with some other random.py. Removing or renaming the file solved the problem.

Solution 9:[9]

In my case, I created a virtual environment named "DL", but the notebook that should have run under this environment did not, so I uninstalled it from the notebook and installed it again with the commands:

jupyter kernelspec uninstall DL

python -m ipykernel install --user --name=DL

So, when I opened the Jupyter notebook, it connected with the DL kernel as before.

Solution 10:[10]

I simply just restarted the Anaconda program and opened a new Python 3 notebook, and Jupyter could connect. :)

Solution 11:[11]

I had the same issue where the kernel would simply not connect. Tried every solution, still no connection.

In the end I noticed some ValueError Exceptions being raised in the console where I started the jupyter client. The source of these exceptions was another python script in the same folder as the notebook.

Fixed the script. Fixed the notebook.

Solution 12:[12]

I experienced a similar issue on my old PC converted into a Ubuntu 20.04 server, with Jupyterlab running under Jupyterhub. The issue appeared after installing Jupytext with the following command:

python3 -m pip install jupytext --upgrade

which non only installed Jupytext but also upgraded my system to Jupyterlab 2.2.9 and Jupyterhub 1.2.2 (among other packages updates).

After that, running a cell containing the simple code:

3 * 4

under any python 3.6 kernel or even R 3.6 or 4.0 kernel left me with the status message:

Kernel xxx connecting...

but never completing the connection nor the simple computation.

Neither re-installing Tornado to version 5.1.1., nor reinstalling ipywidgets, succeeded in solving the issue.

But finally downgrading Jupyterlab and Jupyterhub to (approximately) the original version numbers worked for me:

python3 -m pip install wheel jupyterhub==1.1.0 jupyterlab==2.1.0 ipywidgets

According to the command outputs, the following packages versions were actually applied:

Installing collected packages: wheel, jupyterhub
  Attempting uninstall: wheel
    Found existing installation: wheel 0.34.2
    Uninstalling wheel-0.34.2:
      Successfully uninstalled wheel-0.34.2
  Attempting uninstall: jupyterhub
    Found existing installation: jupyterhub 1.1.0
    Uninstalling jupyterhub-1.1.0:
      Successfully uninstalled jupyterhub-1.1.0
Successfully installed jupyterhub-1.2.2 wheel-0.36.1

And after restarting jupyterhub, all my kernels were working again.

Solution 13:[13]

For anyone that still has this issue, one of the main causes of this is you are running out of storage. Try deleting some files to make some room.

Solution 14:[14]

pip install jupytext --upgrade this work for me.

Solution 15:[15]

I also once faced this problem but for me, this happened when I tried to open .ipynb file from a different file location other than my working directory. But this was solved by moving the required file to my working directory.

So, for your issue just make sure whether your launched new notebook is in your working directory or not.

Solution 16:[16]

for linux and windows:

     just install ipython kernel

           "python3 -m pip install ipykernel"

           "python3 -m ipykernel install --user"

      then restart jupyter notebook that's it