Pragmatic Virtualenvwrapper with a subshell sandbox


Virtualenvwrapper makes working with Virtualenv a real pleasure. But for me it wasn’t enough, just because I’m that lazy.

My idea was to type one command and get right into the repository directory. I’d like it to happen within a subshell so I would not need to deactivate the virtual environment. All I would need to do is press CTRL+D to exit the subshell and close my sandbox.

To accomplish it, I managed to write this scratch shell script function:

function work () {
    typeset env_name="$1"
    if [ "$env_name" = "" ]
    then
        virtualenvwrapper_show_workon_options
        return 1
    fi

    virtualenvwrapper_verify_workon_environment $env_name || return 1

    echo "source ~/.profile
          workon $env_name
          cdvirtualenv
          cd src" > ~/.virtualenvrc

    bash --rcfile ~/.virtualenvrc
}

Having virtualenv and virtualenvwrapper installed on your system, just append this function to the end of your .profile or .bash. And type: work envname.

[]‘s!

, , , , , ,

  • http://chris.improbable.org Chris Adams

    I share your laziness: my standard project config involves something like the following additions to the postactivate file:

        echo "export PROJECT_ROOT=`pwd`" >> $VIRTUAL_ENV/bin/postactivate
        echo 'export DJANGO_SETTINGS_MODULE=project.dev_settings' >> $VIRTUAL_ENV/bin/postactivate
        echo "export PATH=\$PATH:$PROJECT_ROOT/bin" >> $VIRTUAL_ENV/bin/postactivate
        echo "cd `pwd`" >> $VIRTUAL_ENV/bin/postactivate