Reputation: 317
In Python I can create a virtual environment in VS Code with the following commands, I'll also install a kernel to the same virtual environment:
python3 -m venv .venv
source .venv/bin/activate
pip install ipykernel
python3 -m ipykernel install --user --name=.venv
I then go in VS Code and I can Select Kernel
and am given three options:
No matter which I choose it just gives me various ways to select the .venv
environment I just setup. When I am trying to select a kernel why I am being presented with .venv
environments? Aren't kernels and environments two distinct things in Python? I can already operate out of my .venv
environment but can't figure out how to actually select the Jupyter kernel I installed into the .venv
environment.
When I look at tutorials it seems there's supposed to be the phrase Jupyter Server: Local
at the bottom of my VS Code but that's totally missing. I'm not using conda and want to avoid it if possible.
Upvotes: 3
Views: 136
Reputation: 796
Python Environments
.venv
provides the base Python interpreter and tools.Jupyter Kernels:
When you install the ipykernel
package in a virtual environment and register it, you're creating a kernel that can be selected in Jupyter (or VS Code) and will use that environment.
.venv
Environments when selecting a Kernel?The Jupyter extension in VS Code associates environments with kernels because kernels are backed by environments. When you register a kernel using python3 -m ipykernel install
, it's tied to the .venv
environment you created, so it shows up as an option.
In VS Code, the kernel selection menu shows environments because the kernel you're selecting is tied to a specific Python environment. It's a bit confusing but normal behavior.
Ensure Jupyter extension is installed
Verify Jupyter is installed in the Virtual Environment
In your .venv
, install Jupyter - pip install jupyter
Select the .venv
Environment in VS Code
Ctrl+Shift+P
(or Cmd+Shift+P
on macOS)..venv
environment you created.Confirm Kernel is registered
You can check if the kernel is registered by running this in your .venv
- jupyter kernelspec list
, this will show an entry for .venv
.
Ensure Jupyter Server is running locally
Open a notebook or .ipynb
file in VS Code.
At the bottom of VS Code, you should see "Jupyter Server: Local". If not,
.venv
environment.Test Notebook execution
.ipynb
file in VS Code..venv
.Upvotes: 1
Reputation: 47
This used to annoy me a lot, to the point I developed my own solution for it.
I recommend to use pipenv to manage your virtual environments. This is the set up I have.
My workflow is as follows. From the directory I am starting project in, from the terminal, I run
pipenv shell
This will create a virtual environment for you and manage its location for you too.
I then install jupyter-notebook
,Ipython
and some other jupyter-related packages to that specific virtual environment using a script I have developed over my time in industry.
#!/usr/bin/env python
import subprocess
import os
print(f"Current working dirctory: { os.getcwd() }")
pipenv_name = os.environ['VIRTUAL_ENV'].split("/")[-1]
print(f"Installing ipykernel to environment: {pipenv_name}")
python_loc = subprocess.check_output(["which", "python"]).decode("utf-8").rstrip("\n")
pip_loc = subprocess.check_output(["which", "pip"]).decode("utf-8").rstrip("\n")
print("python location: ", python_loc)
print("pip location: ", pip_loc)
print("Install ipykernel to user in environment")
try:
subprocess.check_call([pip_loc,"install","ipykernel", "jupyterlab", "jupyter_nbextensions_configurator", "jupyter-http-over-ws", "ipywidgets", "widgetsnbextension"])
except subprocess.CalledProcessError:
subprocess.check_call(["pip","install", "ipykernel", "jupyterlab", "jupyter_nbextensions_configurator", "jupyter-http-over-ws", "ipywidgets", "widgetsnbextension"])
pass
try:
subprocess.check_call([str(python_loc), "-m", "ipykernel", "install", "--user", f"--name={pipenv_name}"])
except subprocess.CalledProcessError:
subprocess.check_call(["python3","-m","ipykernel","install","--user",f"--name={pipenv_name}"])
pass
I keep this script in my home directory under ~/.install-jpnb.py
and from within my active virtual environment run
~/.install-jpnb.py
Make sure you execute the above script , ~/.install-jpnb.py
, from the terminal inside your virtual environment, before launching vscode
Then, you can open up vscode, and there will be a choice of virtual environments in which you can run jupyter-notebook
s (depending on how many you've previously created under this workflow)
Like in my example screenshot, I have two virtual environments both with all the jupyter-notebook packages install, which I can switch between from just within vscode.
Once you delete the virtual environment using something like
pipenv --rm
you're jupyter-notebook environment associated to that virtual environment will also be removed.
Note I only used pipenv
to manage the environment. I dont use its requirements Pipfile
file or any other features it has, although you are free to explore them. I still just used pip freeze > requirements.txt
to generate the installation requirements.
But nethertheless, pipenv
allowed me to easily work with jupyter-notebook
s across different environments easily.
Upvotes: 1