subprocess.Popen:找不到mkvirtualenv

T. *_*sen 4 python virtualenvwrapper

我在部署中使用virtualenvwrapper.为了设置新环境,我正在运行一个包含所有必需步骤的python脚本.

setupscript包括:

cmd = 'mkvirtualenv %s --no-site-packages'%('testname')
head = subprocess.Popen(cmd,shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
for line in head.stdout.read().splitlines():
    print line
Run Code Online (Sandbox Code Playgroud)

输出是:

/bin/sh: mkvirtualenv: not found
Run Code Online (Sandbox Code Playgroud)

如何在我的python脚本中正确使用virtualenvwrapper?

编辑:

以下代码适用于我:

cmd = 'source /usr/local/bin/virtualenvwrapper.sh && mkvirtualenv %s --no-site-packages'%('testname')
head = subprocess.Popen(cmd, executable='bash', shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
for line in head.stdout.read().splitlines():
    print line
Run Code Online (Sandbox Code Playgroud)

谢谢你的所有答案.

jfs*_*jfs 5

mkvirtualenv可能是一个shell函数,通过virtualenvwrapper.sh从shell的启动文件中获取脚本来添加到您的环境中.在shell=True(例如/bin/sh -c ...)上调用的默认命令可能无法读取它.

您可以显式地获取文件:

import pipes
from subprocess import check_call

check_call("""source /path/to/virtualenvwrapper.sh &&
    mkvirtualenv --no-site-packages """ + pipes.quote(envname),
    executable='bash', shell=True)
Run Code Online (Sandbox Code Playgroud)