Skip to content

Instantly share code, notes, and snippets.

@kennethreitz
Created March 28, 2011 15:23
Show Gist options
  • Save kennethreitz/890647 to your computer and use it in GitHub Desktop.
Save kennethreitz/890647 to your computer and use it in GitHub Desktop.
Automatic de/activating virtualenvs!
export WORKON_HOME=$HOME/.virtualenvs
source /usr/local/share/python//virtualenvwrapper.sh
export VIRTUALENV_USE_DISTRIBUTE="1"
function cd(){
builtin cd "$@"
$(/Users/kreitz/.oh-my-zsh/tools/workon.py)
}
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os
import sys
def find_above(*names):
"""Attempt to locate a .workon file by searching parent dirs."""
path = '.'
while os.path.split(os.path.abspath(path))[1]:
for name in names:
joined = os.path.join(path, name)
if os.path.exists(joined):
return os.path.abspath(joined)
path = os.path.join('..', path)
if __name__ == '__main__':
wo_file = find_above('.workon')
if wo_file and not 'VIRTUAL_ENV' in os.environ.keys():
with open(wo_file) as f:
print('source {0}/bin/activate'.format(f.read()))
elif not wo_file and 'VIRTUAL_ENV' in os.environ.keys():
print('deactivate')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment