Virtualenv激活脚本无法在带有-euo设置的bash脚本中运行 [英] Virtualenv activate script won't run in bash script with set -euo

查看:95
本文介绍了Virtualenv激活脚本无法在带有-euo设置的bash脚本中运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个bash脚本来激活virtualenv,pip安装requirements.txt并继续.这将是我的init.sh脚本,供以后使用.

I am trying to create a bash script that activates the virtualenv, pip installs the requirements.txt and continue. This will be my init.sh script for later business.

#!/usr/bin/env bash

set -euo pipefail

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

其中$ {DIR}设置为包含virtualenv的目录.看来问题出在上述 set -euo 上,根据一些样式指南,这是推荐开始使用bash脚本的起点.更具体地说,它的 u 选项-交互式,产生错误/scripts-venv/bin/activate:第57行:PS1:未绑定变量.我可以删除它,但是只是想知道为什么会这样.谢谢

where ${DIR} is set to my directory that contains the virtualenv. It seems the issue lies in the above set -euo which is the recommended start to bash scripts according to some style guides. More specifically, its the u option - interactive that gives the error /scripts-venv/bin/activate: line 57: PS1: unbound variable. I can remove it, but was just wondering why this is happening. Thanks

推荐答案

如果可以更新,则virtualenv> = 16.2不再有未设置PS1的错误

如果您能够更新 virtualenv 库,则将发现此库现已修复.它已在 pypa/virtualenv/pull/922 中进行了修复,该版本已包含在16.2中.里程碑.

If you can update, virtualenv>=16.2 no longer has errors from PS1 not being set

If you are able to update the virtualenv library you will find this is now fixed. It was fixed in pypa/virtualenv/pull/922, which was included in the 16.2 milestone.

$ PS1 是显示在bash提示中 $ 前面的文本. -u 表示对未绑定变量的引用是错误的.由于/scripts-venv/bin/activate 是指 $ PS1 ,并且由于在交互式shell上没有提示,因此这是一个未绑定的变量,而 -u 导致脚本失败.

$PS1 is the text that appears in front of the $ in your bash prompt. -u says that references to unbound variables are errors. 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.

也许有帮助:

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

调用脚本时,运行该脚本的外壳没有提示.现在,在第57行的 bin/activate 内部查看:

When you call a script, the shell that runs that script doesn't have a prompt. Now, look inside bin/activate, line 57:

_OLD_VIRTUAL_PS1="$PS1" 

您会看到将对 $ PS1 进行评估,并且由于设置了 -u ,因此脚本无法继续,因为 -u 表示尝试对未设置的变量进行参数评估是错误的.

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.

以下是您的一些选择:

第57行:

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

第61行:

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

:-语法使扩展默认为空字符串而不是未绑定,因此没有错误.但这是繁重的工作,因为您搞砸了 virtualenv 创建的代码.

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.

activate 脚本期间删除 -u 可能更好.

Probably its better just to remove -u during the activate script.

尝试使用此脚本,了解我的意思:

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 $?"

通过在激活过程中关闭 -u ,然后再次将其重新打开,您可以解决virtualenv的尴尬(如果您不想修复它).

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

只需更新virtualenv,使其版本> = 16.2. pip install --upgrade virtualenv

Just update virtualenv so it is version >= 16.2. pip install --upgrade virtualenv

这篇关于Virtualenv激活脚本无法在带有-euo设置的bash脚本中运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆