Last Updated: February 25, 2016
· josetomastocino

Fix virtualenvwrapper to work with fabric

Fabric is nice to deploy websites, and virtualenv with virtualenvwrapper are a must to handle dependencies in python projects. When you install virtualenvwrapper you add some lines to your .bashrc file so it can do its magic, something like:

if [ -f /usr/local/bin/ ] 
    source /usr/local/bin/

The problem appears when you try to use the virtualenvwrappers commands from within a fabric task, for example:

def deploy():
    code_dir = '/srv/yourwebsite'
    with cd(code_dir):
        with prefix('workon niceproject'):
            run("git pull")
            run("web/ syncdb")
            run("web/ migrate niceproject_api")

You may get an error like

/bin/bash: workon: command not found

That's because some distros, like Ubuntu, do not execute the code in .bashrc if it's not an interactive session, so the virtualenvwrapper commands are not available. To fix that, move the virtualenvwrapper load code from .bashrc to .bash_profile and then it'll work flawless.

2 Responses
Add your response

Do you know Fabric Virtualenv? It provides a context manager for doing operations inside a virtualenv. You've to know the location of your environment, but know you've to know the name of the environment. So that doesn't make a big difference.

over 1 year ago ·

Nop, I didn't know about fabric-virtualenv. Looks nice, the only problem is the one you comment, you have to know the actual path. To tell you the truth, that's a huge issue to me, because the thing I love the most about virtualenvwrapper is the fact that I can forget about the virtualenv location and just use workon.

I don't think it'd be hard to improve fabric-virtualenv for it to use default locations if you pass just the virtualenv name.

over 1 year ago ·