简体   繁体   English

Virtualenv 激活脚本不会在带有 set -euo 的 bash 脚本中运行

[英]Virtualenv activate script won't run in bash script with set -euo

I am trying to create a bash script that activates the virtualenv, pip installs the requirements.txt and continue.我正在尝试创建一个 bash 脚本来激活 virtualenv,pip 安装 requirements.txt 并继续。 This will be my init.sh script for later business.这将是我用于以后业务的 init.sh 脚本。

#!/usr/bin/env bash

set -euo pipefail

. ${DIR}/scripts-venv/bin/activate
pip install -r requirements.txt

where ${DIR} is set to my directory that contains the virtualenv.其中 ${DIR} 设置为我的包含 virtualenv 的目录。 It seems the issue lies in the above set -euo which is the recommended start to bash scripts according to some style guides.似乎问题出在上面的set -euo ,这是根据一些样式指南推荐的 bash 脚本开始。 More specifically, its the u option - interactive that gives the error /scripts-venv/bin/activate: line 57: PS1: unbound variable .更具体地说,它的u选项 - 交互式给出错误/scripts-venv/bin/activate: line 57: PS1: unbound variable I can remove it, but was just wondering why this is happening.我可以删除它,但只是想知道为什么会发生这种情况。 Thanks谢谢

If you can update, virtualenv>=16.2 no longer has errors from PS1 not being set如果可以更新,virtualenv>=16.2 不再有来自未设置 PS1 的错误

If you are able to update the virtualenv library you will find this is now fixed.如果您能够更新virtualenv库,您会发现现在已修复。 It was fixed in pypa/virtualenv/pull/922 , which was included in the 16.2 milestone.它在pypa/virtualenv/pull/922 中得到修复,它包含在 16.2 里程碑中。


Regarding versions < 16.2 ;关于版本< 16.2 and explanation of what you are seeing并解释你所看到的

$PS1 is the text that appears in front of the $ in your bash prompt. $PS1是出现在 bash 提示中$前面的文本。 -u says that references to unbound variables are errors. -u表示对未绑定变量的引用是错误的。 Since /scripts-venv/bin/activate refers to $PS1 and since there's no prompt on an interactive shell then this is an unbound variable and -u causes the script to fail.由于/scripts-venv/bin/activate指的是$PS1并且由于交互式 shell 上没有提示,因此这是一个未绑定的变量, -u会导致脚本失败。

Maybe this helps:也许这有帮助:

https://unix.stackexchange.com/questions/170493/login-non-login-and-interactive-non-interactive-shells https://unix.stackexchange.com/questions/170493/login-non-login-and-interactive-non-interactive-shells

When you call a script, the shell that runs that script doesn't have a prompt.当您调用脚本时,运行该脚本的 shell 没有提示。 Now, look inside bin/activate , line 57:现在,查看bin/activate ,第 57 行:

_OLD_VIRTUAL_PS1="$PS1" 

You can see that $PS1 is going to get evaluated, and because you have -u set, your script can't continue because -u says an attempt to do parameter evaluation of an unset variable is an error.您可以看到$PS1将被评估,并且因为您设置了-u ,您的脚本无法继续,因为-u表示尝试对未设置的变量进行参数评估是一个错误。

Here are some options for you:这里有一些选项供您选择:

Option 1: You could fix bin/activate选项 1:您可以修复bin/activate

LINE 57:第 57 行:

-   _OLD_VIRTUAL_PS1="$PS1"  
+   _OLD_VIRTUAL_PS1="${PS1:-}" 

Line 61:第 61 行:

-        PS1="(`basename \"$VIRTUAL_ENV\"`) $PS1"
+        PS1="(`basename \"$VIRTUAL_ENV\"`) ${PS1:-}"

The :- syntax causes the expansion to default to an empty string instead of unbound so there's no error. :-语法导致扩展默认为空字符串而不是未绑定,因此没有错误。 But this is heavy handed because you're messing with the virtualenv created code.但这很麻烦,因为您正在弄乱virtualenv创建的代码。

Option 2: Workaround it选项 2:解决方法

Probably its better just to remove -u during the activate script.activate脚本期间删除-u可能更好。

Try this script, to see what I mean:试试这个脚本,看看我的意思:

#!/bin/bash

set -eux
echo "Testing vitualenv"
set +u
. venv/bin/activate
set -u
echo "Test complete $?"

By turning off -u during activate and then turning it back on again you can just work around the virtualenv awkwardness (if you don't want to fix it).通过在激活期间关闭-u然后再次打开它,您可以解决 virtualenv 的尴尬(如果您不想修复它)。


Option 3 [the future!]选项 3 [未来!]

Just update virtualenv so it is version >= 16.2.只需更新 virtualenv,使其版本 >= 16.2。 pip install --upgrade virtualenv

删除set -euo pipefail ......它会工作得很好

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM