Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trouble activating virtualenv on server via Fabric

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:

$ fab staging master pythonver
[myserver.no] Executing task 'master'
[myserver.no] Executing task 'pythonver'
[myserver.no] run: source /home/newsapps/sites/mysite/env/bin/activate && echo "import sys; print sys.path" | python 
[myserver.no] Login password: 
[myserver.no] out: ['', '/usr/lib/python2.6', '/usr/lib/python2.6/plat-linux2', '/usr/lib/python2.6/lib-tk', '/usr/lib/python2.6/lib-old', '/usr/lib/python2.6/lib-dynload', '/usr/lib/python2.6/dist-packages', '/usr/lib/pymodules/python2.6', '/usr/lib/pymodules/python2.6/gtk-2.0', 

As you can see it uses system python and not my virtualenv located in home/newsapps/sites/mysite/env

But if I run this command directly on the server

source /home/newsapps/sites/mysite/env/bin/activate && echo "import sys; print sys.path" | python 

.. then it outputs the right paths from the virtualenv

What am I doing wrong since the commands are not run with the python from my virtualenv using Fabric?

like image 650
Anders E Avatar asked May 09 '11 08:05

Anders E


1 Answers

You should call the python version from your virtualenv bin directory, then you will be sure it uses the virtualenv's version of python.

/home/newsapps/sites/mysite/env/bin/python /home/newsapps/sites/mysite/repository/mysite/configs/staging/manage.py collectstatic --noinput -v0
like image 119
Ken Cochrane Avatar answered Nov 02 '22 19:11

Ken Cochrane