Virtualenv: activate.sh fails if 'nounset' option is set

Created on 7 Jul 2011  ·  24Comments  ·  Source: pypa/virtualenv

I get the following error when I try to activate a virtualenv.

$ source test-env/bin/activate
-bash: _OLD_VIRTUAL_PATH: unbound variable

This is happening because I've configured Bash to use the _nounset_ option, which throws an error when accessing unset variables (see here)

Most helpful comment

Workaround for now—

set -o nounset

[...]

set +o nounset
. ~/.env/bin/activate
set -o nounset

[...]

All 24 comments

I can fix this by changing this line:
if [ -n "$OLD_VIRTUAL_PATH" ] ; then
to:
if [ -n "${_OLD_VIRTUAL_PATH=''}" ] ; then
The ${VAR=DEFAULT} construct returns VAR if it's set, and DEFAULT if it's not (see this page. We can then use the empty string as default, causing the expected behavior. It's a little less readable than I'd like, but it does the trick.

Actually, that's a lame fix, since it requires rewriting all existential tests. It's far easier to just add set -o nounset to the top of the script.

I hit this too.

Me too

I am suspecting that perhaps a way to unset the -u for the innards of the script, and then have it restore the original setting when finishing would make sense.

I'm trying to figure out a way of doing so here - http://stackoverflow.com/questions/13494841/how-can-you-ask-bash-for-the-current-options

I would do:

if [ -n "$OLD_VIRTUAL_PATH" ] ; then

to:

if [ -n "${_OLD_VIRTUAL_PATH-}" ] ; then
${var-DEFAULT}  If var not set, evaluate expression as $DEFAULT *

Yup, just hit this myself.

Running virtualenv==1.11.4.

Workaround for now—

set -o nounset

[...]

set +o nounset
. ~/.env/bin/activate
set -o nounset

[...]

This might be fixed by https://github.com/pypa/virtualenv/pull/723, which uses if ! [ -z "${_OLD_VIRTUAL_PATH+x}" ] ; then.

Please note that $_OLD_VIRTUAL_PATH itself is meant to be removed in #722 though. But #723 fixes it for the other vars, too.

:+1:

I am hitting this too....

It is kinda weird that such a easy problem is not fixed after four years.
beaumartinez's work-a-round is the most simple for now.

This is fixed by #645.

Fixed

@dstufft could you please specify in which release was this fixed so we can assure that we have the minimal needed version installed? From the bug is not clear at all regarding which release includes the fix.

@ssbarnea It was fixed on 12 Aug 2015, as noted in the comments above, so any version released after that date. From https://virtualenv.pypa.io/en/latest/changes/ that means 13.1.1 (and indeed the note for that version mentions this change specifically). All of that information is easily available, so you could probably have found it with a brief search (that's what I did).

I have some bad news: this bug should be reopened because now I get activate: line 13: _OLD_VIRTUAL_PYTHONHOME: unbound variable and while fixing this I think there is critical to introduce a test that does attempt to activate the virtual environment using strict bash

This also applies to line 22: ZSH_VERSION: unbound variable... and wondering how long the list would go... my workaround command line starting to look perverse:

PS1="${{PS1:-}}" _OLD_VIRTUAL_PATH="${{_OLD_VIRTUAL_PATH:-}}" _OLD_VIRTUAL_PYTHONHOME="${{_OLD_VIRTUAL_PYTHONHOME:-}}" source "$VENV/bin/activate"

You can of course just set up the environment yourself, or use the full pathname of the Python executable.

Sorry for reopening the discussion on this, I made the mistake of not checking the versions of virtualenv that we had on the build server and I made an ugly discovery, an ancient version 1.10.1. This would count for lots of bugs. I am going to upgrade it tomorrow.

No apology necessary; In fact I learned a few things from this revived
conversation.

On Tue, Mar 7, 2017 at 3:09 PM, Sorin Sbarnea notifications@github.com
wrote:

Sorry for reopening the discussion on this, I made the mistake of not
checking the versions of virtualenv that we had on the build server and I
made an ugly discovery, an ancient version 1.10.1. This would count for
lots of bugs. I am going to upgrade it tomorrow.


You are receiving this because you commented.
Reply to this email directly, view it on GitHub
https://github.com/pypa/virtualenv/issues/150#issuecomment-284859700,
or mute the thread
https://github.com/notifications/unsubscribe-auth/AAFWbGU02kdSoecXUESrDHlOUN9Rci5Oks5rjcdvgaJpZM4AQ_Js
.

It seems that the bug still exists even in the current release, so I raised it as https://github.com/pypa/virtualenv/issues/1029

Was this page helpful?
0 / 5 - 0 ratings