让Python虚拟环境,Fabric和Sudo一起工作

时间:2013-07-22 19:17:02

标签: python python-2.7 virtualenv zsh fabric

重现的步骤:

  1. 在Linux环境中,在桌面上创建一个名为“fab”
  2. 的文件夹
  3. 仅供参考,我正在使用zsh
  4. cd to ~/Desktop/fab
  5. 通过发出fabfile.py运行此./fab目录中的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')
    
  6. 收到此错误:

    ➜  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.
    
  7. 从zsh运行source ./virtualenv/venv/bin/activate && pip install mysql-python django South,并观察它是否有效。

  8. 这不是this之类的重复问题,因为即使我在代码中使用with prefix(,我也会收到同样的错误。

    有什么想法吗?

1 个答案:

答案 0 :(得分:3)

source替换为/bin/bash/。这是一个例子:

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)

仅供参考,对于run/sudo,最好使用prefix上下文管理器,例如建议的here

希望有所帮助。