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.
Edit:
I learned that
mkvirtualenv
is a shell function. In this case the question becomeshow to run shell function in python
. My answer below can be applied to standalone binaries. For your question, please look at the answer: https://stackoverflow.com/a/5826523/1906700 You can indirectly call mkvirtualenv() function from the script that defines it.Set PATH variable correctly
The problem seems to be caused by your
$PATH
variable. You need to correctly set this variable so thatmkvirtualenv
executable can be found. For example, if you havemkvirtualenv
executable in `/home/thore/scripts, you need to set your $PATH in .bashrc or .zshrc (depending on your shell) as follows:In that way,
mkvirtualenv
script will be found correctly and run.Another Solution
The other solution to the problem would be using the exact path for the script in question. In that case, you can give
/home/thore/scripts/mkvirtualenv
as a parameter to subprocess.mkvirtualenv
might be a shell function that is added to your environment by sourcingvirtualenvwrapper.sh
script from your shell's startup file. The default command invoked onshell=True
(e.g.,/bin/sh -c ...
) might not read it.You could source the file explicitly: