3

Steps to reproduce:

  1. In a linux environment, create a folder on your desktop called 'fab'
  2. FYI, I'm using zsh
  3. cd to ~/Desktop/fab
  4. Run this fabfile.py made in your ./fab dir by issuing fab init:

    #!/usr/bin/env python
    #set up ssh to remote server
    
    import sys, os, fileinput
    from fabric.api import *
    
    def init():
        local('mkdir ./virtualenv')
        local('cd ./virtualenv && virtualenv --no-site-packages venv')
        local('chown -R user:user ./virtualenv/')
        local('chmod 770 -R ./virtualenv/')
        venv = 'source ./virtualenv/venv/bin/activate && '
        local(venv+'pip install mysql-python django South')
    
  5. Get this error:

    ➜  fab  fab init    
    [localhost] local: mkdir ./virtualenv
    [localhost] local: cd ./virtualenv && virtualenv --no-site-packages venv
    New python executable in venv/bin/python
    Installing distribute...........................................................................................................................................................................................................................done.
    Installing pip................done.
    [localhost] local: chown -R user:user ./virtualenv/
    [lcalhost] local: chmod 770 -R ./virtualenv/
    [localhost] local: source ./virtualenv/venv/bin/activate && pip install mysql-python django South
    /bin/sh: 1: source: not found
    
    Fatal error: local() encountered an error (return code 127) while executing 'source ./virtualenv/venv/bin/activate && pip install mysql-python django South'
    
    Aborting.
    
  6. Run source ./virtualenv/venv/bin/activate && pip install mysql-python django South from zsh, and observe that it works.

This is not a duplicate question of something like this, as I am getting the same error even if I use a with prefix( in my code.

Any ideas?

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
mh00h
  • 1,824
  • 3
  • 25
  • 45

1 Answers1

3

Replace source with /bin/bash/. Here's an example:

from fabric.api import *


def init():
    local('virtualenv --no-site-packages venv')

    venv_command = '/bin/bash venv/bin/activate'
    pip_command = 'venv/bin/pip install mysql-python django South'
    local(venv_command + ' && ' + pip_command)

FYI, for run/sudo it's better to use prefix context manager, like suggested here.

Hope that helps.

Community
  • 1
  • 1
alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
  • 1
    breaking up the commands into two variables like that also made a difference. – mh00h Jul 22 '13 at 20:00
  • This kind of works, but not completely. `pip_command` projects are not installing- the system now just tries to install to the system, but not the virtual environment (even if `fab init` is run as a non-root user). – mh00h Jul 22 '13 at 23:24
  • 2
    How about using `venv/bin/pip` instead of just `pip`? – alecxe Jul 23 '13 at 06:45
  • 1
    the `pip_command` is being run outside the virtualenv. Structure it [like so](http://stackoverflow.com/a/18397763/456848): `local("/bin/bash -l -c 'source venv/bin/activate && python manage.py runserver'")` – Dave Aug 23 '13 at 08:05