72

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?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Henrik Leijon
  • 1,277
  • 2
  • 10
  • 15

18 Answers18

90

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

msw
  • 42,753
  • 9
  • 87
  • 112
rocksteady
  • 1,697
  • 14
  • 18
35

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/

Nimantha
  • 6,405
  • 6
  • 28
  • 69
otmane42
  • 535
  • 4
  • 11
17

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

Nimantha
  • 6,405
  • 6
  • 28
  • 69
Swordopolis
  • 307
  • 2
  • 3
9

Encounter the same behaviour. Python code works perfectly fine, but Jupyter refuses to pick up the local .venv.

  • The local venv is in Python: Select Interpreter but not in Jupyter's Select kernel list.

  • The problem is there're too many venv in the system!

If you encounter the same behaviour,

  • Press F1, then Jupyter: Filter kernels, uncheck everything, except the local env.
  • Then F1 -> Developer: Reload Window. Jupyter will automatically use the default local venv.
FisNaN
  • 2,517
  • 2
  • 24
  • 39
8

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).

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
tauft
  • 546
  • 4
  • 13
6

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",

Michel Kluger
  • 164
  • 1
  • 6
4

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.

EDIT (2 Aug 2023): Now it's no longer necessary to restart VSCode, or to manually pip install ipykernel.

P i
  • 29,020
  • 36
  • 159
  • 267
  • For macOS, I find this the most simple solution. Also, note that 1: you create a virtual environment in `venv` or `.venv` 2: and when you select kernel, you might have multiple suggestions of which `venv/bin/python` is a relative path. – Domenico Spidy Tamburro Aug 01 '23 at 08:47
3

The only thing that worked for me was choosing venv in the upper right corner of VS Code: Choosing python venv for Jupyter Notebook in VS Code

anamitr
  • 49
  • 1
  • 3
  • 1
    Python: Select Interpreter option was not working, but the option you have shown solved the issue. – ncelik Feb 25 '23 at 21:22
  • I tried that. It doesn't give me the option of selecting any of my virtual environments. It will let me create a new one ... but then afterwards it isn't selected. And can't be. – hartshoj May 28 '23 at 18:59
2

Here is how to do for venv with Jupyter Notebook on VSCode in Windows:

  1. Create a venv and get the path to this venv in Windows. As an example, with Anaconda, I get: C:\Users\rascoussier\Anaconda3\envs\research310.

  2. 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.

  3. Restart VSCode.

  4. Now launch command pallet(ctrl+shift+P) and run >Notebook: Select Notebook kernel. Normally the venv python should be available and it should then works.

Onyr
  • 769
  • 5
  • 21
2

Make sure you have installed jupyter, notebook, ipykernel libraries in your virtual environment.

Then hit Ctrl + Shift +P , press >Python: Select Interpreter and choose your path of the venv.

After that, hit Ctrl + Shift +P again, run >Notebook: Select Notebook kernel.

If you have already opened the jupyter notebook window , reload it again and you can find your path of the venv in Jupyter's Select kernel list.

easontseng
  • 29
  • 2
0

Try a few things:

  1. Make sure you can run the code from a Visual Studio Code terminal using the "ipython" prompt with the same Conda environment.
  2. If it works then sometimes it is a caching issue, so close your file and open a new one.
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Syed Amir Raza
  • 101
  • 1
  • 1
0

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.

See the final output image

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
0

If this happens while using WSL server, don't forget to install Python in the WSL as well, because it doesn't come automatically from the local installation to the server. https://code.visualstudio.com/docs/remote/wsl-tutorial

0

Another alternative is to specify the folders where the environmental variables should be sought for.

Create your virtual environment using conda create --name ENV_NAME e.g conda create --name pwd Then, conda activate pwd

It Should print out details like this:

enter image description here

Use that environment location

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 Folders (See the image below)

enter image description here

olawalejuwonm
  • 1,315
  • 11
  • 17
0

Using vscode on ubuntu here. For me the solution was to run the command

F1 -> Notebook: Select Notebook Kernel

Then Select the venv I had set up. The default was Xonsh and was pointing to the system python.

Paul P M
  • 141
  • 1
  • 9
0

The solution that worked for me was:

  1. Launching Jupyter Notebook locally and copying the localhost URL.
  2. In VS Code, Ctrl + Shift + P -> Jupyter: Select Interpeter or Start Jupyter Server, then pasting the URL.

After these steps, the venv interpreter showed up as a Jupyter Kernel, allowing to select and run the code.

Interestingly enough, after this step, other python interpreters also appeared. Check JasonZHM's response New Virtual environments not listed in Kernels until i reload VS Code for the original solution.

Karen
  • 401
  • 7
  • 15
0

I had the same problem, but I didn't have to install a jupyter kernel as other answers suggest, although I installed the jupyter extension.

What I did is to open the folder containing the notebook with vscode. By doing that, the previously activated virtual environment was available in the kernel list.

  • This does not provide an answer to the question. Once you have sufficient [reputation](https://stackoverflow.com/help/whats-reputation) you will be able to [comment on any post](https://stackoverflow.com/help/privileges/comment); instead, [provide answers that don't require clarification from the asker](https://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-can-i-do-instead). - [From Review](/review/late-answers/34868900) – Ram Chander Aug 24 '23 at 15:06
  • @RamChander: Why don’t you think this provides an answer to the question? I’d prefer more detail, but it’s clearly proposing a course of action. – Jeremy Caney Aug 26 '23 at 23:22
-2

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
John Calchon
  • 119
  • 6