Kernel not connecting in Jupyter Lab

Question:

The kernel says connecting and then says No Kernel | Disconnected.
I have clean-uninstalled anaconda and uninstalled all jupyter using
python -m pip uninstall -y jupyter jupyter_core jupyter-client jupyter-console jupyterlab_pygments notebook qtconsole nbconvert nbformat

Searching anaconda or jupyter in Home directory gave nothing.

Then, I fresh installed anaconda and still the problem stays.

At first, it seemed kernel was connecting with a new notebook but after some time kernel was connecting in a new notebook.
(Update: I found out that the new notebooks are connecting to kernel until an old notebook is opened. Once an old notebook is opened, then onwards, no notebooks are connecting to kernel)

I’m stuck on this.

The following is what I get in the terminal:

[I 2021-08-01 21:01:42.797 ServerApp] nbclassic | extension was successfully linked.
[I 2021-08-01 21:01:42.846 LabApp] JupyterLab extension loaded from /home/username/anaconda3/lib/python3.8/site-packages/jupyterlab
[I 2021-08-01 21:01:42.846 LabApp] JupyterLab application directory is /home/username/anaconda3/share/jupyter/lab
[I 2021-08-01 21:01:42.849 ServerApp] jupyterlab | extension was successfully loaded.
[I 2021-08-01 21:01:42.852 ServerApp] nbclassic | extension was successfully loaded.
[I 2021-08-01 21:01:42.852 ServerApp] Serving notebooks from local directory: /home/username
[I 2021-08-01 21:01:42.853 ServerApp] Jupyter Server 1.4.1 is running at:
[I 2021-08-01 21:01:42.853 ServerApp] http://localhost:8888/lab?token=3d46a4259293e2afc5f584cf2d62586c5c553fd37e55e775
[I 2021-08-01 21:01:42.853 ServerApp]  or http://127.0.0.1:8888/lab?token=3d46a4259293e2afc5f584cf2d62586c5c553fd37e55e775
[I 2021-08-01 21:01:42.853 ServerApp] Use Control-C to stop this server and shut down all kernels (twice to skip confirmation).
[C 2021-08-01 21:01:42.877 ServerApp] 
    
    To access the server, open this file in a browser:
        file:///home/username/.local/share/jupyter/runtime/jpserver-9804-open.html
    Or copy and paste one of these URLs:
        http://localhost:8888/lab?token=3d46a4259293e2afc5f584cf2d62586c5c553fd37e55e775
     or http://127.0.0.1:8888/lab?token=3d46a4259293e2afc5f584cf2d62586c5c553fd37e55e775
[I 2021-08-01 21:01:48.429 LabApp] Build is up to date
[W 2021-08-01 21:01:49.469 ServerApp] Notebook Desktop/Link to foldername/ms-p/peroo/anl.ipynb is not trusted
[I 2021-08-01 21:01:52.346 ServerApp] Kernel started: 7bbe2308-8b30-4a4b-8b04-31aabe33d635
Traceback (most recent call last):
  File "/home/username/anaconda3/lib/python3.8/runpy.py", line 194, in _run_module_as_main
    return _run_code(code, main_globals, None,
  File "/home/username/anaconda3/lib/python3.8/runpy.py", line 87, in _run_code
    exec(code, run_globals)
  File "/home/username/anaconda3/lib/python3.8/site-packages/ipykernel_launcher.py", line 15, in <module>
    from ipykernel import kernelapp as app
  File "/home/username/anaconda3/lib/python3.8/site-packages/ipykernel/__init__.py", line 2, in <module>
    from .connect import *
  File "/home/username/anaconda3/lib/python3.8/site-packages/ipykernel/connect.py", line 13, in <module>
    from IPython.core.profiledir import ProfileDir
  File "/home/username/anaconda3/lib/python3.8/site-packages/IPython/__init__.py", line 55, in <module>
    from .core.application import Application
  File "/home/username/anaconda3/lib/python3.8/site-packages/IPython/core/application.py", line 25, in <module>
    from IPython.core import release, crashhandler
  File "/home/username/anaconda3/lib/python3.8/site-packages/IPython/core/crashhandler.py", line 27, in <module>
    from IPython.core import ultratb
  File "/home/username/anaconda3/lib/python3.8/site-packages/IPython/core/ultratb.py", line 112, in <module>
    from IPython.core import debugger
  File "/home/username/anaconda3/lib/python3.8/site-packages/IPython/core/debugger.py", line 47, in <module>
    from pdb import Pdb as OldPdb
  File "/home/username/anaconda3/lib/python3.8/pdb.py", line 77, in <module>
    import code
  File "/home/username/Desktop/Projects/foldername/ms-p/peroo/code.py", line 535, in <module>
    "execution_count": null,
NameError: name 'null' is not defined
[I 2021-08-01 21:01:55.348 ServerApp] AsyncIOLoopKernelRestarter: restarting kernel (1/5), new random ports
Traceback (most recent call last):
  File "/home/username/anaconda3/lib/python3.8/runpy.py", line 194, in _run_module_as_main
    return _run_code(code, main_globals, None,
  File "/home/username/anaconda3/lib/python3.8/runpy.py", line 87, in _run_code
    exec(code, run_globals)
  File "/home/username/anaconda3/lib/python3.8/site-packages/ipykernel_launcher.py", line 15, in <module>
    from ipykernel import kernelapp as app
  File "/home/username/anaconda3/lib/python3.8/site-packages/ipykernel/__init__.py", line 2, in <module>
    from .connect import *
  File "/home/username/anaconda3/lib/python3.8/site-packages/ipykernel/connect.py", line 13, in <module>
    from IPython.core.profiledir import ProfileDir
  File "/home/username/anaconda3/lib/python3.8/site-packages/IPython/__init__.py", line 55, in <module>
    from .core.application import Application
  File "/home/username/anaconda3/lib/python3.8/site-packages/IPython/core/application.py", line 25, in <module>
    from IPython.core import release, crashhandler
  File "/home/username/anaconda3/lib/python3.8/site-packages/IPython/core/crashhandler.py", line 27, in <module>
    from IPython.core import ultratb
  File "/home/username/anaconda3/lib/python3.8/site-packages/IPython/core/ultratb.py", line 112, in <module>
    from IPython.core import debugger
  File "/home/username/anaconda3/lib/python3.8/site-packages/IPython/core/debugger.py", line 47, in <module>
    from pdb import Pdb as OldPdb
  File "/home/username/anaconda3/lib/python3.8/pdb.py", line 77, in <module>
    import code
  File "/home/username/Desktop/Projects/foldername/ms-p/peroo/code.py", line 535, in <module>
    "execution_count": null,
NameError: name 'null' is not defined
[I 2021-08-01 21:01:58.362 ServerApp] AsyncIOLoopKernelRestarter: restarting kernel (2/5), new random ports
Traceback (most recent call last):
  File "/home/username/anaconda3/lib/python3.8/runpy.py", line 194, in _run_module_as_main
    return _run_code(code, main_globals, None,
  File "/home/username/anaconda3/lib/python3.8/runpy.py", line 87, in _run_code
    exec(code, run_globals)
  File "/home/username/anaconda3/lib/python3.8/site-packages/ipykernel_launcher.py", line 15, in <module>
    from ipykernel import kernelapp as app
  File "/home/username/anaconda3/lib/python3.8/site-packages/ipykernel/__init__.py", line 2, in <module>
    from .connect import *
  File "/home/username/anaconda3/lib/python3.8/site-packages/ipykernel/connect.py", line 13, in <module>
    from IPython.core.profiledir import ProfileDir
  File "/home/username/anaconda3/lib/python3.8/site-packages/IPython/__init__.py", line 55, in <module>
    from .core.application import Application
  File "/home/username/anaconda3/lib/python3.8/site-packages/IPython/core/application.py", line 25, in <module>
    from IPython.core import release, crashhandler
  File "/home/username/anaconda3/lib/python3.8/site-packages/IPython/core/crashhandler.py", line 27, in <module>
    from IPython.core import ultratb
  File "/home/username/anaconda3/lib/python3.8/site-packages/IPython/core/ultratb.py", line 112, in <module>
    from IPython.core import debugger
  File "/home/username/anaconda3/lib/python3.8/site-packages/IPython/core/debugger.py", line 47, in <module>
    from pdb import Pdb as OldPdb
  File "/home/username/anaconda3/lib/python3.8/pdb.py", line 77, in <module>
    import code
  File "/home/username/Desktop/Projects/foldername/ms-p/peroo/code.py", line 535, in <module>
    "execution_count": null,
NameError: name 'null' is not defined
Asked By: darthV

||

Answers:

It looks like you have a conflict in your PYTHONPATH. pdb attempts to import code, but instead of getting its module it sees your /home/username/Desktop/Projects/foldername/ms-p/peroo/code.py file which leads to the failure. Rename this file to something else, e.g. mycode.py. This can be a common problem, e.g. you cannot have a file named statistics.py because you will shadow built-in statistics module. This is well explained in answers to: Importing installed package from script raises "AttributeError: module has no attribute" or "ImportError: cannot import name".

Answered By: krassowski

Just make *.py files and *.IPYNB files in a separated folders.
Done.

Answered By: Dr. Alaa Temimy

I have found this same issue, and identified it only happens with Chrome standard browser. Incognito Chrome or Firefox does not have the same issue of ipykernel not connecting.

For now I open jupyter in firefox using the following command:

jupyter lab --browser Firefox

Not sure what the difference is with the browsers, but recently I have had an issue of not being able to access local files from chrome, so will look into this issue and see if that resolves it for Chrome standard browser.

Answered By: Nelson da Silva