How to use python virtual environments in VS Code jupyter notebooks integration?

Question:

I have created a virtual environment with venv as follows from VS Code integrated terminal:

python3 -m venv <name>

after cd in the directory, I activated it by doing source <name>/bin/activate

How can I make it available for the kernel of my VS Code integration of jupyter notebook? I only see the default python installations as possible kernels i.e. usr/bin/python3

Also, let’s say I would like to then revert the process and remove that kernel – how can I do it?

Asked By: G. Macia

||

Answers:

Create a new python environment via the console, then from a notebook try and select it as a kernel it doesn’t appear in the list of python environments. This phenomenon also occurs in my VS Code.

There’s an issue in github, Python extension team will see if there’s enough upvotes, then they will consider this feature request in future planning. You may upvote the issue.

Answered By: Molly Wang-MSFT

As mentioned by Molly Wang below, the kernel does not appear in the list of Python environments and you should upvote this issue in Github. As a workaround, you could open the python interpreter menu in VS Code (crtl+p) and type "Python interpreter" and then navigate to your venv python (choose the right version within the /bin folder in your venv)

enter image description here

Answered By: G. Macia

My hacky workaround – create a new .ipynb in the same directory, VSC will find the interpreter, then you can go back to your original notebook and select it there.

For whatever reason I’m finding this is happening frequently for me now with Notebooks that used to never have a problem.

Answered By: Major Major