python3 virtual env aliases

less than 1 minute read

So I recently transitioned from using conda environments to python virtual environments. They’re pretty similar but I’ve written a couple scripts to make things easier for myself.

Installation steps/notes:

  1. If you’re on a mac you have to install python 3 because they still come with python 2 for some reason…
    • No included package manager? Install homebrew
    • brew install python3
  2. I prefer to keep all my venvs in one location ~/envs/
    • mkdir envs
    • cd envs
    • python3 -m venv whatever_venv_name_you_want
  3. Add a script to .bashrc or .bash_profile to make activation aliases
    • This script makes activate-name_of_venv an alias that points to the correct bash command for all envs in ~/envs/
      for D in ~/envs/*/
       alias activate-$N="source $D/bin/activate"
    • source .bashrc or restart your terminal
  4. Install ipykernel for jupyter hub or notebooks
    • activate-name_of_venv
    • pip install ipykernel
    • python -m ipykernel install --user --name name_of_venv --display-name "name_of_venv"