'Jupyter notebooks in Visual Studio Code does not use the active virtual environment
I write Python code in Visual Studio Code and run the program from a terminal in which I have activated a virtual environment, and it works fine.
However, if I create notebook cells using #%% and run those interactively, the virtual environment is not used. How can I fix this?
Solution 1:[1]
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
(This correction comes from a comment to Jupyter Notebook is loading incorrect Python kernel #2563.)
*and don't forget to restart VSCode
Solution 2:[2]
I find it easy to use pipenv install ipykernel
to set up the virtual environment with the Jupyter kernel in one go (comment on rocksteady's answer).
Solution 3:[3]
All you need is to edit Vscode settings following these steps:
- Open
Open User settings
using 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 kernel
using shortcut Ctrl + Shift + P
Read more here: https://techinscribed.com/python-virtual-environment-in-vscode/
Solution 4:[4]
for me solved by adding the path of my venv to the settings.json,
now the kernel is detected automatically
"python.pythonPath": "P:\Miniconda3_64bit\venv\Scripts\python.exe",
Solution 5:[5]
For VSCode, your Jupyter kernel is not necessarily using the same python interpreter you're using at the command line.
Use Ctrl + Shift + P to open the Command Palette, and select "Notebook: Select Notebook Kernel"
Then choose the interpreter you're using at the terminal
Solution 6:[6]
Try a few things:
- Make sure you can run the code from a Visual Studio Code terminal using the "ipython" prompt with the same Conda environment.
- If it works then sometimes it is a caching issue, so close your file and open a new one.
Solution 7:[7]
Now let me show you a scenario. You select the interpreter in Visual Studio Code, and then you write codes below '# %%'. The moment you hit Ctrl + Enter, you are guessing that the IPython kernel that Visual Studio Code is using is not of the interpreters that you have selected. In this case you could write the following code to conform which interpreter is used for IPython kernel.
import sys
print(sys.executable)
This shows the executable path that the IPython kernel is using. If you see that it's not taking the correct interpreter then here's something that worked for me.
Just restart your computer. Then reopen Visual Studio Code and reselect the interpreter and again hit Ctrl + Enter. Now this time Visual Studio Code should take the correct interpreter and its IPython kernel.
Solution 8:[8]
On macOS I have .venv/ in the same folder as my .ipynb
. .venv/bin/activate
pip install ipykernel
Then I restart VSCode in the project folder, open the notebook, and in the Select Kernel
dropdown I see .venv/bin/python
Selecting that, now it works.
Solution 9:[9]
Here is how to do for venv with Jupyter Notebook on VSCode in Windows:
Create a venv and get the path to this venv in Windows. As an example, uith Anaconda, I get:
C:\Users\rascoussier\Anaconda3\envs\research310
.Now, we need to tell VSCode to use it. In VSCode, go to the Python Extension > Extension Settings. Search for Python: Venv Path. Add the path where the venvs are located. Here we added
C:\Users\rascoussier\Anaconda3\envs\research310
.Restart VSCode.
Now launch command pallet and run
>Notebook: Select Notebook kernel
. Normally the venv python should be available and it should then works.
Solution 10:[10]
Try installing the Anaconda Extension pack.
When I code in Visual Studio Code with this extension in the bottom left corner, I can select the virtual environment I want to execute my code in. Hence installing this package should make the trick.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow