本文介绍了使Python虚拟环境,Fabric和Sudo协同工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

复制步骤:

  1. 在Linux环境中,在桌面上创建一个名为"fab"的文件夹
  2. 仅供参考,我正在使用zsh
  3. 从CD到~/Desktop/fab
  4. 运行在您的./fab目录中制作的fabfile.py,方法是发出fab init:

  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')

  • 出现此错误:

  • 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.
    

  • 从zsh运行source ./virtualenv/venv/bin/activate && pip install mysql-python django South,并观察它是否有效.

  • Run source ./virtualenv/venv/bin/activate && pip install mysql-python django South from zsh, and observe that it works.

    这不是重复的问题,例如,因为即使在代码中使用with prefix(,我也遇到相同的错误.

    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.

    有什么想法吗?

    推荐答案

    source替换为/bin/bash/.这是一个示例:

    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)
    

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

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

    希望有帮助.

    这篇关于使Python虚拟环境,Fabric和Sudo协同工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

  • 08-13 11:31