Skip to content

Instantly share code, notes, and snippets.

@pinealan
Created January 23, 2020 11:16
Show Gist options
  • Save pinealan/d26f01bc187e6ef621be0d22576bf655 to your computer and use it in GitHub Desktop.
Save pinealan/d26f01bc187e6ef621be0d22576bf655 to your computer and use it in GitHub Desktop.
Bash snippet for easy access to venv
venv() {
# use global venvs from $HOME if arg is provided
if [[ -n $1 ]]; then
. ~/venv/$1/bin/activate
return
fi
# look for a venv in current directory
for dir in "venv" ".venv" "env" ".env"; do
if [[ -d $dir ]]; then
. $dir/bin/activate
return
fi
done
# exit with error
echo ERROR: Unable to find local environment >&2
return 127
}
@pinealan
Copy link
Author

Project specific virtual environments

$ which python
/usr/bin/python
$ ls
.venv/ src/ setup.py server.py ...
$ venv
$ (.venv) which python
/home/you/project/.venv/bin/python3

System wide virtual environments

$ which python
/usr/bin/python
$ venv scipy-env
$ (scipy-env) which python
/home/you/venv/scipy-env/bin/python3

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