subprocess.Popen: mkvirtualenv not found

北战南征 提交于 2019-11-28 13:57:15

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)
Eren T.

Edit:

I learned that mkvirtualenv is a shell function. In this case the question becomes how 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 that mkvirtualenv executable can be found. For example, if you have mkvirtualenv executable in `/home/thore/scripts, you need to set your $PATH in .bashrc or .zshrc (depending on your shell) as follows:

 export PATH=$PATH:/home/thore/scripts

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!