bash shell 函数如何知道它是否在 virtualenv 中运行?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15454174/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can a shell function know if it is running within a virtualenv?
提问by kjo
How should a bashfunction test whether it is running inside a Python virtualenv?
bash函数应该如何测试它是否在 Python virtualenv 中运行?
The two approaches that come to mind are:
想到的两种方法是:
[[ "$(type -t deactivate)" != function ]]; INVENV=$?
or
或者
[[ "x$(which python)" != "x$VIRTUAL_ENV/bin/python" ]]; INVENV=$?
(Note: wanting $INVENVto be 1 if we're inside a virtualenv, and 0 otherwise, is what forces the backward-looking tests above.)
(注意:$INVENV如果我们在 virtualenv 中,则希望为 1,否则为 0,这将强制执行上述向后测试。)
Is there something less hacky?
有什么不那么hacky的吗?
采纳答案by kjo
Actually, I just found a similar question, from which one can easily derive an answer to this one:
其实,我刚刚发现了一个类似的问题,从中可以很容易地得出这个问题的答案:
Python: Determine if running inside virtualenv
E.g., a shell script can use something like
例如,shell 脚本可以使用类似
python -c 'import sys; print (sys.real_prefix)' 2>/dev/null && INVENV=1 || INVENV=0
(Thanks to Christian Long for showing how to make this solution work with Python 3 also.)
(感谢 Christian Long 展示了如何使这个解决方案也适用于 Python 3。)
EDIT: Here's a more direct (hence clearer and cleaner) solution (taking a cue from JuanPablo's comment):
编辑:这是一个更直接(因此更清晰、更干净)的解决方案(从 JuanPablo 的评论中得到提示):
INVENV=$(python -c 'import sys; print ("1" if hasattr(sys, "real_prefix") else "0")')
回答by robertklep
if [[ "$VIRTUAL_ENV" != "" ]]
then
INVENV=1
else
INVENV=0
fi
// or shorter if you like:
[[ "$VIRTUAL_ENV" == "" ]]; INVENV=$?
EDIT: as @ThiefMaster mentions in the comments, in certain conditions (for instance, when starting a new shell – perhaps in tmuxor screen– from within an active virtualenv) this check may fail (however, starting new shells from within a virtualenv may cause other issues as well, I wouldn't recommend it).
编辑:正如@ThiefMaster 在评论中提到的,在某些情况下(例如,当启动一个新的 shell 时——可能是在活动的 virtualenv中tmux或screen——从活动的 virtualenv 中)此检查可能会失败(但是,从 virtualenv 中启动新的 shell 可能会导致其他也有问题,我不推荐它)。
回答by Kurt
If you use virtualenvwrappers there are pre/post scripts that run that could set INVENV for you.
如果您使用 virtualenvwrappers,则运行前/后脚本可以为您设置 INVENV。
Or what I do, put the following in your your .bashrc, and make a file called .venv in your working directory (for django) so that the virtual env is automatically loaded when you cd into the directory
或者我所做的,将以下内容放入您的 .bashrc 中,并在您的工作目录(对于 django)中创建一个名为 .venv 的文件,以便在您 cd 进入目录时自动加载虚拟环境
export PREVPWD=`pwd`
export PREVENV_PATH=
handle_virtualenv(){
if [ "$PWD" != "$PREVPWD" ]; then
PREVPWD="$PWD";
if [ -n "$PREVENV_PATH" ]; then
if [ "`echo "$PWD" | grep -c $PREVENV_PATH`" = "0" ]; then
deactivate
unalias python 2> /dev/null
PREVENV_PATH=
fi
fi
# activate virtualenv dynamically
if [ -e "$PWD/.venv" ] && [ "$PWD" != "$PREVENV_PATH" ]; then
PREVENV_PATH="$PWD"
workon `basename $PWD`
if [ -e "manage.py" ]; then
alias python='python manage.py shell_plus'
fi
fi
fi
}
export PROMPT_COMMAND=handle_virtualenv

