Updated 2023.01.18 - Started 2023.01.15
Problem description: Jupyter in VS Code got stuck at connecting to Python 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:
My Python version was 3.10.6 64-bit:
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:
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),
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,
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:
Open Command Palette (Shift+Ctrl+P) or
Select Jupyter: Specify Jupyter Server for Connections, and select Existing:
Put in the box the URI (Uniform Resource Identifier) of the existing server obtained from running jupyter-notebook above, i.e.,
Press Enter.
Close the current Jupyter interactive window and Run Cell 1 of my Python code 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:
Click on the Settings button for Jupyter, and select Install Another Version:
Reload this older Jupyter extension, and do NOT update:
Click on Run Cell 1, but Jupyter window did not open up:
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.