Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
208 views
in Technique[技术] by (71.8m points)

python - subprocess.Popen: mkvirtualenv not found

I'm using virtualenvwrapper in my deployment. To setup new environments, I'm running a python script, which contains all needed steps.

The setupscript includes:

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

The output is:

/bin/sh: mkvirtualenv: not found

How can I correctly use virtualenvwrapper within my python script?

EDIT:

The following code works for me:

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

Thanks for all answers.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

mkvirtualenv might be a shell function that is added to your environment by sourcing virtualenvwrapper.sh script from your shell's startup file. The default command invoked on shell=True (e.g., /bin/sh -c ...) might not read it.

You could source the file explicitly:

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)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...