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 OverflowI 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.
Videos
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
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/
I’m learning Python to do the data work I normally do with R and have been using VSCode. Since I don’t come from a comp sci background, there are some things that are pretty foreign to me, but I’m slowly getting a grasp of them.
I think I figured out virtual environments, but I’m confused on selecting a Python interpreter and Jupyter kernel in VScode - do I have to select the Python interpreter from my virtual environment, or can I just use whatever VSCode recommends? And do I really have to do this every time I open up VSCode?
Edit: also, my virtual environment has 3 Python interpreters I can choose - python, python3, and python3.12. What do I choose and why?
And what the hell is a Jupyter kernel? Do kernels always need to be installed in the same place as the virtual environment I’m using?
TIA, y’all were incredibly helpful in my last post
Hello Everybody!
I am using VS Code to program in python / sympy, which works no problem. However, when I shut down VS Code and turn it back on, there are no kernels to select from. VS Code recommends me to install python, but I already have it installed.
I have tried many different approaches with the help of ChatGPT, but the only solution I have found to work is to completely delete everything VS Code from my computer and redownload it. But this approach just isn't viable if I have to do it every time I want to code.
Any help is greatly appreciated!
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.
I have problems in getting Jupyter Notebook to use a virtual environment I created. With just Python code, venv works great. But when I use Jupyter Notebook, it does not accept the venv environment I choose. In other words, after I do Control-Shift-P and select Python virtual environment from a location, it still uses the system Python version. The following is a screenshot.
First, I launch Visual Studio Code (VSCode), establish a virtual environment (venv), and set up the necessary directories. I then proceed to install the required dependencies. When it comes to executing a .pynb file within this venv, I can employ a command-line instruction to view and interact with the Jupyter notebook in Google Chrome, allowing me to run the cells seamlessly.
However, when I attempt to open the same notebook in VSCode, I encounter an issue: the venv environment I created does not appear as an option for running the file.
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.
I'm working with vscode to read jupyter notebook file. Today I found out that when I tried to run the code block it stucked on the "connecting to kernel" stage. I wonder that what's going on and how to fix it.
Python: 3.7.4
Vscode: 1.64.0