cvjdiw
Last Updated: February 25, 2016
·
3.731K
· ericmarcos

Django deployment with Fabric + virtualenvwrapper

Fabric and virtualenvwrapper are two extremely useful tools if you're working with Django and dealing with a whole bunch of different deployments and environments. However, using them both together is a bit tricky as Fabric uses sh instead of bash when using the local() api. The problem with this is that if you try to activate your virtualenv it will fail with a "/bin/sh: source: command not found" error.

I have a small function that implements the workaround posted here(1):

def django_manage(command='help', virtualenv='local-django'):
    return "/bin/bash -l -c 'source /usr/local/bin/virtualenvwrapper.sh && workon " + virtualenv + " && python src/manage.py " + command + "'"

and I use it like this:

local(django_manage("migrate app --noinput"))

(to make this work you'll have to make your virtualenv postactivate cd into your project directory)

Hope you find it useful!

(1) http://stackoverflow.com/questions/8005348/why-doesnt-fabric-see-my-bash-profile