Skip to content

Instantly share code, notes, and snippets.

@kstrauser
Created December 6, 2017 18:31
Show Gist options
  • Save kstrauser/88f958b73b0725d2a63f854dc19a94fd to your computer and use it in GitHub Desktop.
Save kstrauser/88f958b73b0725d2a63f854dc19a94fd to your computer and use it in GitHub Desktop.
Cheap hack to emulate virtualenvwrapper's workon script
function workon () {
# Find the configured virtualenvwrapper home, or use Kirk's
# clearly excellent choice.
if [[ -z $PROJECT_HOME ]]; then
envs=$PROJECT_HOME
else
envs=~/Envs
fi
# Override the paths for specific environments
case $1 in
data_builders)
cd ~/Envs/data-pipelines/data_builders
;;
*)
cd ~/Envs/$1
;;
esac
# Open a pipenv shell if possible
if [[ -e Pipfile ]]; then
pipenv shell
fi
}
@kstrauser
Copy link
Author

I've switched from using virtualenvwrapper to pipenv as it better fits my workflow. I really missed that lovely workon command, though. This is my attempt at more or less emulating its behavior:

$ which python
/usr/local/bin/python

$ python --version
Python 2.7.14

$ workon data_builders
Spawning environment shell (/usr/local/bin/zsh). Use 'exit' to leave.
source /Users/kirk/.local/share/virtualenvs/data_builders-ulboz1a4/bin/activate

$ source /Users/kirk/.local/share/virtualenvs/data_builders-ulboz1a4/bin/activate

$ which python
/Users/kirk/.local/share/virtualenvs/data_builders-ulboz1a4/bin/python

$ python --version
Python 3.6.3

Notes

I use Zsh, but this also works in Bash.

I create all my virtualenvs in ~/Envs. Set $PROJECT_HOME to your own preferred path (which you've probably already done if you'd previously used virtualenvwrapper).

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