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.
screenshot for reference
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-notebooks (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-notebooks across different environments easily.

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With