It's because there is an extra step needed - you need to explicitly install a Jupyter kernel that points to your new Python virtual environment. You can't simply activate Jupyter-lab or Notebook from the virtual environment. This has tripped me up before, too.
Follow the advice here: Using Jupyter notebooks with a virtual environment
And, in fact, there can be an issue where your kernel still doesn't point to the correct Python binary, in which case you need to change one suggestion in the above advice process:
From: ipython kernel install --user --name=projectname
To: python3 -m ipykernel install --user --name=projectname
where projectname is the name of the venv to use as the ipython kernel.
(This correction comes from a comment to Jupyter Notebook is loading incorrect Python kernel #2563.)
*and don't forget to restart VSCode
Answer from rocksteady on Stack OverflowIt's because there is an extra step needed - you need to explicitly install a Jupyter kernel that points to your new Python virtual environment. You can't simply activate Jupyter-lab or Notebook from the virtual environment. This has tripped me up before, too.
Follow the advice here: Using Jupyter notebooks with a virtual environment
And, in fact, there can be an issue where your kernel still doesn't point to the correct Python binary, in which case you need to change one suggestion in the above advice process:
From: ipython kernel install --user --name=projectname
To: python3 -m ipykernel install --user --name=projectname
where projectname is the name of the venv to use as the ipython kernel.
(This correction comes from a comment to Jupyter Notebook is loading incorrect Python kernel #2563.)
*and don't forget to restart VSCode
All you need is to edit Vscode settings following these steps:
- Open
Open User settingsusing shortcut Ctrl + Shift + P - Type in search space "env"
- Under Extentions -> Python , you will find Python: Venv Path
- Type the absolute path to your enviroment "path/to/myenv/bin" in linux or "path/to/myenv/Script/"
- Restart vsCode
- Select the desired kernel using
Notebook : Select Notebook kernelusing shortcut Ctrl + Shift + P
Read more here: https://techinscribed.com/python-virtual-environment-in-vscode/
uv .venv and VS Code Jupyter Notebook: Connecting to Kernel
No kernel dropdown when trying to run Jupyter in VSCode
New Virtual environments not listed in Kernels until i reload VS Code
VS Code does not find Python kernel - Stack Overflow
Videos
I can't see the venv in my current working directory in this list:
which comes up when I click on select kernel on the top right
The python interpreter is set to the one I want. So everything else works as expected.
I even tried pressing ctrl+shift+P and using the "Select Interpreter to start Jupyter Server". There my venv appears, but clicking it seems to have no effect on the other way (above) of selecting the kernel.
Hi. I'm trying to run some code in a Jupyter notebook in VSCode. Whenever I try to do this, I get a pop up that asks me to select a kernel. I also see this is written on the top right of the window I'm in. The problem is that there is nothing in this dropdown menu, and I therefore cannot run anything.
I am running VSCode with the Python and Jupyter extensions installed. I've created a virtual environment and installed ipykernel. I've also tried to install jupyter to see if that helped, but it didn't. And yes, the environment is selected (by opening the menu in VSCode, selecting "Select Python interpreter" and picking the Python-binary in .venv). I've rebooted my machine and reinstalling the extensions, yet it does not solve the issue. Running normal python files works fine.
Any tips? Cheers.
Had to update the Jupyter extension, even though I had auto-update on.
I had a lot of problems myself in getting the updated list of jupyter kernel servers in old versions of Visual Studio code.
However, in the newer version of Visual Studio code (I am using version 1.76.2 on my ubuntu 18.04 machine; I hope this works also on Mac OS), you don't need to specify the jupyter kernel anymore: it is now enough to specify the python environment. This new feature and its use are described in this howto from visualstudio.
In short, once I installed the correct version of Visual Studio code, I performed the following steps:
- I first make a virtual environment and install ipykernel in it:
virtualenv --python=python3 venv . venv/bin.activate pip3 install ipykernel
- In Visual Studio code, while having opened a python file, I select using ctrl+shift+P (Shift+Command+P in Mac OS) the python interpreter (Python:Select Interpreter) and choose the one of the virtual environment that I have just created: /path/to/venv/bin/python. In my case, this step was necessary: indeed, I first had to choose the desired python interpreter on any python file before proceeding to the following step
- In Visual Studio code still, while having opened a notebook file (.pynb file), I click on select kernel on the top right of the IDE, then click on "Python Environments": the environment I selected before for the python file (/path/to/venv/bin) will appear here, without having to add environments to the kernel lists. Once this environment is selected, the notebook should work as expected,
I hope it helps.