Skip to content

Instantly share code, notes, and snippets.

@CloudCray
Created February 3, 2020 15:50
Show Gist options
  • Save CloudCray/d412c80a92e07da35645d6e0fea5b0f6 to your computer and use it in GitHub Desktop.
Save CloudCray/d412c80a92e07da35645d6e0fea5b0f6 to your computer and use it in GitHub Desktop.
Bash script to activate virtual environments by name
#!/bin/bash
#
# virtualenvs must be in `~/venv`
# save this as /usr/local/bin/activate
# file is sourced instead of executed - need read permissions only
# `sudo chmod 644 /usr/local/bin/activate`
#
VIRTUAL_ENV="/home/$USER/venv/$1"
export VIRTUAL_ENV
ACT_SCRIPT='source $VIRTUAL_ENV/bin/activate'
eval "$ACT_SCRIPT"
@CloudCray
Copy link
Author

I typically keep all my venvs in the same directory, rather than within the projects themselves. This script helps me swap between them quickly without having to remember any paths or do any directory voodoo. Just source activate data_science or source activate flask_project from anywhere

Uses /home/$USER/venv instead of ~/venv in case file is saved as root

Running deactivate should exit the venv without any additional config

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment