Python VS code does not connect with IPython kernel
Asked Answered
T

6

3

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?

Tailstock answered 31/8, 2020 at 21:4 Comment(2)
you mean vs code could not find jupyter notebook in ur env venv? you better use anaconda for virtual env and launch vs code from anaconda navigator. this will save u horrible headaches one for all timesTurnery
@Tailstock When I use the virtual environment created by python that comes with Anaconda, Jupyter Notebook can be connected to the IPython kernel and used. In addition, you could try to check the installation of related modules by referring to the content of this link. link: github.com/microsoft/vscode-python/issues/5197Propose
M
7

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.

Microclimatology answered 2/9, 2020 at 3:4 Comment(0)
S
7

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

Stalag answered 1/9, 2020 at 17:40 Comment(0)
M
7

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.

Microclimatology answered 2/9, 2020 at 3:4 Comment(0)
N
3

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:\Users\USER\Documents\VS 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.

Nosewheel answered 15/1, 2023 at 14:48 Comment(0)
P
2

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

Phlegethon answered 20/9, 2022 at 3:33 Comment(1)
I don't think "but the ipykernel and tornado is incompatible with ipykernel and tornado" is what you meant to put in your answer. Please edit to help others understand how this addresses the question asked.Daemon
R
0

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.

Rivy answered 16/1, 2023 at 13:12 Comment(1)
I tried this method, but it did not work for me. When I tried to Run a cell, nothing happened. The Jupyter window did not open up. So I stick to Method 2 as described above in #63678670Nosewheel
A
0

2024 september, had the same problem when loading a local virtual environment with Python 3.10.12. The problem persisted when using VSCode with different projects and folders. Got stuck in loading the kernel for an hour and I shut it down.

I tried to do a clean re install first (first time opened VSCode it managed to load, then after closing VSCode problem persisted)

  • Deleted VSCode cache
  • Uninstalled Python 3.10.12 and reinstalled 3.10.3 if there was a problem with versions
  • Cleared pip's cache
  • Restarted the computer

What worked for me: I installed jupyter and at the same time I downgraded the versions of my Jupyer and Python extensions.

  • Uninstalled both
  • Installed the last release from 2023 from Jupyter and the last release from 2023 from Python, which came with Pylance. No special reason on 2023 just choosing something from the past that may be easier to reference.

After reloading VSCode it loaded correctly and was able to run code.

Artificer answered 12/9 at 16:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.