15

I am running VS Code on a Mac OS. I have installed Jupyter extension but I'm unable connect to my Python virtual environments. When I create a new virtual environment with Anaconda, VS Code appropriately recognizes the virtual environments but the ones I create manually do not work.

I have no problem launching Jupyter Notebook in the browser from the command line but I'd like to be able to use VS Code. Also, I have no issues running plain Python scripts in the virtual environment I have created.

I have tried the following steps:

  1. Create a new virtual environment:

    cd ~/.virtuaenvs/
    python3 -m venv new_venv
    
  2. Activate the new virtual environment:

    source new_venv/bin/activate
    
  3. Install ipykernel and jupyter

    pip install ipykernel jupyter
    
  4. Add environment to kernels list

    python3 -m ipykernel install --name "new_venv" --user
    
  5. Restart VS Code. But the new_venv does not show in my list of kernels when opening a Jupyter notebook.

11 Answers 11

13

Had to update the Jupyter extension, even though I had auto-update on.

Sign up to request clarification or add additional context in comments.

Comments

8

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.

1 Comment

You just made my day!
1

For some upgrading to the current jupyter extension may still not work I had to find the correct combination of vscode and jupyter extensions.

I am on older mac OSX 10.13 vscode : 1.78.2 I had been using jupyter extension : 2023.1.2000312134 which would not recognize kernels installed via conda in the vscode workspace using :

    my_vs_workspace me$ conda create --prefix my_conda_env python=3.10
    my_vs_workspace me$ conda activate /my_vs_workspace/my_conda_env
    (my_conda_env) my_vs_workspace me$ python3.10 -m ipykernel install --name "my_conda_env" --user

now reload the vscode workspace and the kernel would still not be available in vscode. Upgrading to the latest jupyter extension v 2023.4.1011241018 would crash the extension and the kernel loading. vscode would recognize the kernel but during the attempt to load an error would be thrown ... bisect the extension and kernel (paraphrase -sorry i didn't write it down).

Loading the jupyter extension : v2023.1.2010391206 does work for me. Reload vscode was required after the terminal commands and the extension changes (just as a reminder)

thanks to those above phi

Comments

1

I just had the same issue when I couldn't get the newly created environment to show up in kernel selection window. It appeared there immediately after I selected my venv/bin/python3 as Python interpreter on the notebook file.

Comments

1

Python must be in the PATH, to at least get the prompt to install the ipykernel package (also required), which any user can do.

(Note that the python installer doesn't update the HKLM Uninstall registry when installing for all users.)

Vscode 1.102.0 was a problem for me, and version 1.105.0 fixed it.

Of course, the Jupyter extension has to be installed.

2 Comments

In regards to "Actually, I'm still having problems, but I suspect python must be in the PATH, to at least get the prompt to install ipykernel, which any user can do.": There was an issue introduced recently. Perhaps this is related to why you are encountering issues?
I didn't even get that far. It looks like vscode 1.102.0 is also a problem, and 1.105.0 fixes it, as long as python is in the PATH.
0

Had this same issue and in my case it was caused by my VS code operating in restricted mode: https://code.visualstudio.com/docs/editor/workspace-trust#:~:text=You%20will%20also%20see%20a,%2C%20workspace%20settings%2C%20and%20extensions. The issue was solved when disabling it.

1 Comment

While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - From Review
0

If your Python came from the Microsoft Store, VS Code/Jupyter may not find kernels. Install Python from python.org, recreate the venv, and register the kernel. That worked for me.

Comments

-1

I did the same in my vscode. Unfortunately, I can get the kernel directly from the list.

I suggest you manually add the virtual environment path in the settings.json file (use shortcuts "Ctrl+shift+P" and type Preference: Open user settings(JSON)):

"python.venvPath": "${fileDirname}/new_venv/Scripts/python",

5 Comments

Thank you very much! I tried this to no avail. The virtual environment shows up in the kernel list on startup for a few seconds (also without adding the line above) but then it disappears.
Will the same problem occur when trying to use the old version of jupyter extension?
Yes, unfortunately.
As an experiment, I just installed VS Codium with homebrew and everything worked right out of the box. I must have messed up something with my VS Code settings files... I also tried reinstalling VS Code but some settings files are probably laying around somewhere because that didn't do the trick.
After all, the official documents can solve 99% of the problems, but your situation seems special. I suggest you raise this bug to the developers in github
-1

I had the same problem. The steps below worked on VSCode v1.84.2 (Universal)

  1. Press Command+Shift+P and run Notebook: Select Notebook Kernel enter image description here
  2. Click on Select Another Kernel enter image description here
  3. Click on Python environments enter image description here
  4. Then go to Create Python Environment enter image description here
  5. Select venv and then Enter interpreter path enter image description here
  6. Enter the relative or absolute path to your venv. enter image description here

That should do it!

1 Comment

I will update this with a command line solution if I ever found one. The cause of this bug is unknown, too.
-1
  1. You need to download Jupyter extension in vs code for wsl again (also If you had downloaded it on windows). It will find the path of python kernel autometically.

  2. Then Run .ipynb file..

1 Comment

that's not about macOS for sure
-1

When I moved to this OS, I was trying this as well. I remember that i had installed Python from the official site which is python.org, but I did not face this issue.

I suggest you update the python version on your system, ensure you installed the suitable version, and retry connecting to the virtual env via VS code. I suggest to follow the tip that was shared earlier in the following link, to reinstall, or update the Jupyter extension, and then relaunch your VS code software.

Also some quick checklist:

  1. ensure ipykernel is installed in your system (if not, install it)

  2. you can try registering the env by yourself on the kernel.

  3. ensure kernel is selected correctly inside your VS code app.

  4. if all above 3 points are satisfied, close the app and restart VS code.

Would love to hear if there's any more effective way to combat this. In addition to above, you can read the attached official documentation of VS code, with link to py environments section.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.