Python VS code does not connect with IPython kernel

Question:

I have created a new virtual env and trying to connect with the VS code. When I open the folder in VS code that contains the file and virtual env, on the left bottom side I can see the python interperator selected as shown in the following figure:

enter image description here

However, when I run the program, it shows the message that it is connecting to IPython Kernel as shown in the following picutre

enter image description here

However, after some time it throws an error message that "unable to start session for kernel python" as shown in the following picture:

enter image description here

The top right corner of VS code is shown in the following picture:

enter image description here

Could anyone help me in fixing the issue?

Asked By: user2293224

||

Answers:

An issue was opened today in the Python VS Code repository that describes a similar problem. Their recommended fix is to close VS Code and then revert the traitlets dependency to version 4.3.3 with this command:

python -m pip install 'traitlets==4.3.3' --force-reinstall

Answered By: Simm Enley

I had the same issue starting yesterday with two new virtual environments and different python versions (3.8.1 and 3.8.5). I can confirm that Simm Enley’s suggested terminal command worked for me.

python -m pip install 'traitlets==4.3.3' --force-reinstall

It has to be executed from within the corresponding virtual environment. A newer version of traitlets (5.0.0) installed by VSCode into the new virtual environment in order to to run the Interactive Terminal caused the issue, it seems like.

Answered By: Jim

the reason is newer version of traitlets (5.0.0) installed by VSCode into the new virtual environment;
but the ipykernel and tornado is incompatible with ipykernel and tornado;
run the code in Terminal:
pip install –upgrade ipykernel
pip install –upgrade tornado

Answered By: ouyang Goman

Updated 2023.01.18 – Started 2023.01.15

Problem description: Jupyter in VS Code got stuck at connecting to Python kernel
Connecting to kernel

I was using VS Code version 1.74.3 (user setup) with the Release Python extension version v2022.20.2, having the version details shown below:

VS Code About details
Python extension Release version details

My Python version was 3.10.6 64-bit:

Python version details

I tried all methods mentioned in all of the above answers, and none worked for me to get Jupyter unstuck.

I ran the Python 3.10.6 setup to repair it, but that did not work:
Repair Python 3.10.6

I updated various packages, but that did not work either:

  • pip install –upgrade ipykernel
  • pip install –upgrade traitlets
  • pip install –upgrade tornado

Some details from the above upgrades:

  • Requirement already satisfied: traitlets>=5.4.0
  • Requirement already satisfied: tornado>=6.1
  • (from ipython>=7.23.1->ipykernel)

Method 1: After uninstalling and reinstalling VS Code, Jupyter worked as before.

When I installed the Pre-Release Python extension (by mistake),
Python extension Pre-Release version details

Jupyter got stuck again. I uninstalled the Pre-Release Python extension, and re-installed the Release Python extension shown in the image above; Jupyter worked again.

The problem with Method 1 is that once VS Code was ended and reopened, the same problem occurred again. Use Method 2 below.

Method 2: Run jupyter-notebook, connect to Jupyter server

In addition to the Jupyter-related extensions that came with installing the Python extension,

Packages installed with Python extension
Jupyter notebook renderers
Jupyter notebook support

install jupyter:

> pip install jupyter
... cut ...
Successfully installed jupyter-1.0.0

Run jupiter-notebook in a VS Code terminal:

> jupyter-notebook
[I 11:47:56.970 NotebookApp] Serving notebooks from local directory: C:UsersUSERDocumentsVS Code - learn
[I 11:47:56.971 NotebookApp] Jupyter Notebook 6.5.2 is running at:
[I 11:47:56.971 NotebookApp] http://localhost:8888/?token=3e14829cf931c6aa61474c740ddf09eb34bd457f3dba20b3
[I 11:47:56.971 NotebookApp]  or http://127.0.0.1:8888/?token=3e14829cf931c6aa61474c740ddf09eb34bd457f3dba20b3
[I 11:47:56.971 NotebookApp] Use Control-C to stop this server and shut down all kernels (twice to skip confirmation).
[C 11:47:57.000 NotebookApp] 

To access the notebook, open this file in a browser:
    file:///C:/Users/USER/AppData/Roaming/jupyter/runtime/nbserver-13876-open.html
Or copy and paste one of these URLs:
    http://localhost:8888/?token=3e14829cf931c6aa61474c740ddf09eb34bd457f3dba20b3
 or http://127.0.0.1:8888/?token=3e14829cf931c6aa61474c740ddf09eb34bd457f3dba20b3
[W 11:48:36.001 NotebookApp] Forbidden
[W 11:48:36.001 NotebookApp] 403 GET /api/sessions?1673977715999 (127.0.0.1) 1.000000ms referer=None
[W 11:48:37.425 NotebookApp] Forbidden
[W 11:48:37.426 NotebookApp] 403 GET /api/kernels?1673977717423 (127.0.0.1) 1.000000ms referer=None
... cut ...

Jupyter notebook opened up in my browser; I closed this Jupyter notebook tab since I did not need it:
Jupyter notebook in web browser

Open Command Palette (Shift+Ctrl+P) or
Open VS Code Command Palette

Select Jupyter: Specify Jupyter Server for Connections, and select Existing:
Select existing Jupyter server

Put in the box the URI (Uniform Resource Identifier) of the existing server obtained from running jupyter-notebook above, i.e.,
Jupyter server URI

Press Enter.

Close the current Jupyter interactive window and Run Cell 1 of my Python code again:
Run Cell 1 again

Problem solved.

The next day, just repeat Method 2 after running VS Code again.

Method 3: Downgrade Jupyter extension

Based on the suggestion in the answer Python VS code does not connect with IPython kernel just below my own answer here, I downgraded the Jupyter extension from v2022.11.1003412109 to v2022.9.1303220346, but it did not work. Below are the details (since I could no longer edit my comment to that answer).

Open up the list of extensions:
Open up VS Code list of extensions

Click on the Settings button for Jupyter, and select Install Another Version:
Click on Settings button of Jupyter extension
Select an older version of Jupyter extension

Reload this older Jupyter extension, and do NOT update:
Reload and ignore update

Click on Run Cell 1, but Jupyter window did not open up:
Run Cell 1

Nothing happened. I uninstalled jupyter package, same problem.

So I stick to Method 2 above with the latest Jupyter extension (not downgraded), and everything was working again.

Answered By: Luke V

I had the same problem and none of the solutions above worked for me.
I succeded to resolve the problem by dowgrading Jupyter version from v2022.11.1003412109 to v2022.9.1303220346 in the extension manager.

Answered By: lm29