I have a error handler script that shows my important environment variables when I fail (I added a few Enter in the echo string for easier reading, the full line is added at the end of the question):
echo -e "--- failed at line $1 in function $2\nvirtual env =
$(echo ${VIRTUAL_ENV})\ncurrent dir = ${PWD}\nbranch = $(git status | head
-1)\ngit commit = $(git log -1 | tr '\n''\t')\npip version = $(pip -
V)\nPy version = $(python --version)\nhostname = $(logname)@$(hostname)\n"
I get a good result, except for the python --version part:
Python 2.7.12 <--- why is this here?
--- failed at line 186 in function my_func
virtual env =
current dir = /home/bla/blabla/foo
branch = On branch goo
git commit = commit ### Author: ...
pip version = pip 18.1 from /usr/local/lib/python2.7/dist-packages/pip-18.1-py2.7.egg/pip (python 2.7)
Py version = <----- instead of here
hostname = X@Y
The actual function of mine doesn't use echo right away. I also call tput setaf
&& tput sgr0
to modify the colors. The python line (the 1st) is not colored, while the others are...
"--- failed at line $1 in function $2\nvirtual env = $(echo ${VIRTUAL_ENV})\ncurrent dir = ${PWD}\nbranch = $(git status | head -1)\ngit commit = $(git log -1 | tr '\n''\t')\npip version = $(pip -V)\nPy version = $(python --version)\nhostname = $(logname)@$(hostname)\n"
Thanks @steeldriver (nice band btw) for his comment. The fix was easier once I verified it was indeed printing to stderr - just redirect stderr to stdout for the command. i.e.
Py version = $(python --version 2>&1)
Also thanks to @pLumo for this comment, that this error (printing to stderr instead stdout) was fixed in python 3.4 and newer