Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
A snippet to automatically call virtualenvwrapper's "workon" command when entering a directory in the shell. This can be added to your .profile, .bash_profile, .bashrc, etc.
# Call virtualenvwrapper's "workon" if .venv exists. This is modified from--
# which is linked from--
check_virtualenv() {
if [ -e .venv ]; then
env=`cat .venv`
echo "Found .venv in directory. Calling: workon ${env}"
workon $env
venv_cd () {
builtin cd "$@" && check_virtualenv
# Call check_virtualenv in case opening directly into a directory (e.g
# when opening a new tab in
# Add the following to ~/.bash_aliases:
# alias cd="venv_cd"

This comment has been minimized.

Copy link
Owner Author

@cjerdonek cjerdonek commented Nov 21, 2013

It would be good to update this so that venv_cd wraps cd if cd has already been aliased (without falling into the recursion trap pointed out here by @dorkitude).


This comment has been minimized.

Copy link

@clneagu clneagu commented Dec 16, 2013

workon should only be called if the venv isn't already initialized

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