Virtual environments in Python, also, for Jupyter notebooks.

jupyter pip python venv virtual-environments

This is the Python newbie section. I am tired of looking this up and I want a recipe to remember, so that is what will be here.

Generate virtual environment for a particular python version

I use the approach proposed by the Bite Code! newsletter author(s), to only use pip and venv.

So the steps are:

  • find out your python version python --version,
  • create a virtual environment with that python version, here 3.10, $PYTHON-VERSION -m venv $VENV-NAME,
  • activate the virtual environment, source $VENV-NAME/bin/activate,
  • first, upgrade pip, python -m pip install --upgrade pip,
  • then, install into the virtual environment with pip, python -m pip install $PACKAGE-NAME,

Sometimes:

  • export the current state of the environment, python -m pip freeze > $REQUIREMENTS-FILENAME.txt,
  • deactivate the environment, deactivate,
  • install the current state of this environment into another, python -m pip install -r $REQUIREMENTS-FILENAME.txt.

And below is what all of that looks like on my machine.

:~/virtualEnvironments$ python --version
Python 3.10.10
:~/virtualEnvironments$ python3.10 -m venv .mlEnv2024-04-17
:~/virtualEnvironments$ source .mlEnv2024-04-17/bin/activate
(.mlEnv2024-04-17) :~/virtualEnvironments$ python -m pip install --upgrade  pip
Requirement already satisfied: pip in ./.mlEnv2024-04-17/lib/python3.10/site-packages (22.3.1)
Collecting pip
  Using cached pip-24.0-py3-none-any.whl (2.1 MB)
Installing collected packages: pip
  Attempting uninstall: pip
    Found existing installation: pip 22.3.1
    Uninstalling pip-22.3.1:
      Successfully uninstalled pip-22.3.1
Successfully installed pip-24.0
(.mlEnv2024-04-17) :~/virtualEnvironments$ python -m pip install numpy
Collecting numpy
  Using cached numpy-1.26.4-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.metadata (61 kB)
Using cached numpy-1.26.4-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (18.2 MB)
Installing collected packages: numpy
Successfully installed numpy-1.26.4
(.mlEnv2024-04-17) :~/virtualEnvironments$ python -m pip freeze > mlEnv2024-04-17-requirements.txt
(.mlEnv2024-04-17) :~/virtualEnvironments$ deactivate

Create a script to activate this virtual environment for a different directory

This is suggested by the Bite Code! newsletter is to create a couple environments that you use with some regularity, so you don’t have to set one up for each little script or project. I want to do that, but I also want to activate the environment from anywhere and not have to remember the exact path. Ok, time for some shell scripting, I’ll be brief here and leave links for further investigations.

The steps are:

  • create a directory within your home directory that you can put in the PATH,
  • update the PATH,
  • create an executable shell script with the path of the virtual environment,
  • then call the script from anywhere with . shell-script


Create a directory within your home directory

I used /bin, so the full path is $HOME/bin.


Update the PATH

You’ll need to add the directory to the existing path and then update ~/.bashrc, link.

export PATH="$PATH:$HOME/bin"

source ~/.bashrc


Create your script

The scripting part is easy, but the actual internals are sort of tricky, link.

My script is mlEnv2024-04-17.sh in $HOME/bin.

mlEnv2024-04-17.sh:

#!/bin/bash

source "$HOME/virtualEnvironments/.mlEnv2024-04-17/bin/activate"

Then change the permissions: chmod a+x mlEnv2024-04-17.sh.


Go somewhere else to test,

$ . mlEnv2024-04-17.sh
(.mlEnv2024-04-17) $ 

which gives me the reassuring (.mlEnv2024-04-17) so I know I am using the right virtual environment.

Use a virtual environment with a Jupyter notebook

It may not have been a good idea to include all of this in one post. I’m tired, aren’t you? And yet, there’s more …

(.mlEnv2024-04-17)$ python -m pip install ipykernel
(.mlEnv2024-04-17)$ ipython kernel install --user --name=.mlEnv2024-04-17
Installed kernelspec .mlEnv2024-04-17 in /......./.local/share/jupyter/kernels/.mlenv2024-04-17
(.mlEnv2024-04-17) $ jupyter notebook

a link on this topic, Vicki Boykis.

Once you’re in the notebook, you may need to select the kernel via Kernel->Change Kernel.

© Amy Tabb 2018 - 2026. All rights reserved. The contents of this site reflect my personal perspectives and not those of any other entity.