Alex Rivera | Logout

Trouble activating virtualenv on server via Fabric

Asked 2011-05-09T08:33:05.193
10

I am trying to run some Django management commands via Fabric on my staging server.

The problem is it seems Fabric is not able to activate the virtualenv and thus using system python/libs when executing the commands.

On the server the Django app is run using a virtualenv (no, I don' use virtualenvwrapper yet...)

Using Fabric (1.0.1) a command might look like this when run from my box:

The fabfile method:

def collectstatic():
    require('settings', provided_by=[production, staging])

    with settings(warn_only=True):
        run('source %(env_path)s/bin/activate && python %(repo_path)s/%(project_name)s/configs/%(settings)s/manage.py collectstatic --noinput -v0' % env)

The output:

$ fab staging master collectstatic
[myserver.no] Executing task 'master'
[myserver.no] Executing task 'collectstatic'
[myserver.no] run: source /home/newsapps/sites/mysite/env/bin/activate && python /home/newsapps/sites/mysite/repository/mysite/configs/staging/manage.py collectstatic --noinput -v0
[myserver.no] Login password: 
[myserver.no] out: Unknown command: 'collectstatic'
[myserver.no] out: Type 'manage.py help' for usage.

I know of course that the Django command collectstatic does not exist in versions prior to 1.3 which leads med to think that system python (which has Django 1.2) is beeing used.

My fabfile/project layout is based on the great fabfile of the Tribapps guys

So I created a fabric method to test pythonversion:

def pythonver():
    require('settings', provided_by=[production, staging])

    with settings(warn_only=True):

    run('source %(env_path)s/bin/activate && echo "import sys; print sys.path" | python ' % env)

When run it gives the following output:

Edit
Report

1 Answer

4

I wouldn't bother with activating the virtualenv, just give the full path to the virtualenv's python interpreter. That will then use the correct PYTHONPATH, etc.

answered 2011-05-09T10:23:18.677

Your Answer