PS1 for Bash prompt showing last exit status, written mostly in Python
The typo

If we look at the beginning and the of the posted code:

PS1="\$(export LAST_RETURN_VALUE="\$?" WHOAMI="\$\(whoami\)" HOSTNAME ; python -c \"
from __future__ import print_function
... more python code
\" "


The end part is incorrect.
The \" closes the python -c \",
and the final " closes the PS1=",
but a ) is missing to close the \$(export.
(Pro tip: if you edit the script in vim, and press % on an opening (, it shows where's the closer, or emits an error beep if missing.)

I guess the last line was meant to be:

\")"


An easier way to escape stuff

Escaping things correctly within "..." can be quite painful in very long strings as in this example.
In this particular example there's easy alternative using literal here documents:

PS1=$(cat &lt;&lt; "EOF"
...
EOF
)


All the code you previously had within the "..." could go there in place of the ..., and it will be taken literally as if enclosed within single-quotes, but no need to escape ", or ' or $.

PS1=$(cat &lt;&lt; "EOF"
$(export LAST_RETURN_VALUE="$?" WHOAMI="$(whoami)" HOSTNAME ; python -c "
from __future__ import print_function
import os 
import datetime

ESC = '\033[{0}m'
esc = ESC.format

OFF, BOLD, UNDERSCORE, BLINK = 0, 1, 4, 5
BLACK, BLACKBG = 30, 40
RED, REDBG = 31, 41
GREEN, GREENBG = 32, 42
YELLOW, YELLOWBG = 33, 43
BLUE, BLUEBG = 34, 44
MAGENTA, MAGENTABG = 35, 45
CYAN, CYANBG = 36, 46
WHITE, WHITEBG = 37, 47

LAST_RET = os.environ['LAST_RETURN_VALUE']

print(esc(YELLOW) + datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S'),
      esc(GREEN) + os.environ['WHOAMI'] + esc(RED) + '@' +
      esc(CYAN) + os.environ['HOSTNAME'],
      esc(BLUE) + esc(BOLD) + os.getcwd())
if LAST_RET != '0':
    print(esc(OFF) + esc(MAGENTABG) + LAST_RET + esc(OFF), end='')
print(esc(OFF) + '$ ', end='')
"
)
EOF
)


Safety

If PROMPT_COMMAND is set, PS1 will be ignored.
To avoid confusion, I would add unset PROMPT_COMMAND.

What's wrong with doing this in Python?

It's an additional dependency to worry about.
If your friend asks you how to do that cool prompt,
it gets less cool when you explain how he needs Python to get it.
And running a Python script for every single command is a bit of a waste.

Why not do it in Bash?

The script basically sets variables, formats the date and prints stuff.
That's easy enough to do in Bash.
The script gets all its critical elements from Bash.
It uses the Bash variable $? to get the exit code of the last command,
the whoami command to get the current user,
and the HOSTNAME variable that's already defined in the environment.

In other words,
you already know everything it takes to make this work in Bash,
and Python doesn't bring real benefits,
the only thing left to do is to translate it to Bash.

This is pretty much a word-for-word translation of Python to Bash,
with some minor improvements, such as \u instead of calling whoami,
and unused variables dropped.
I don't see the readability suffering significantly compared to the Python solution.

PS1=$(cat &lt;&lt; "EOF"
$(
LAST_RET=$?
WHOAMI='\u'
HOSTNAME='\h'
DATE=$(date +'%Y-%m-%d %H:%M:%S')

OFF='\033[0m'
BOLD='\033[1m'
RED='\033[31m'
GREEN='\033[32m'
YELLOW='\033[33m'
BLUE='\033[34m'
MAGENTABG='\033[45m'
CYAN='\033[36m'

echo "$YELLOW$DATE $GREEN$WHOAMI$RED@$CYAN$HOSTNAME $BLUE$BOLD$PWD"
test $LAST_RET = 0 || printf "$OFF$MAGENTABG$LAST_RET$OFF"
printf "$OFF\$ "
)
EOF
)