7

I created a venv like so:

python3 -m venv .venv

When I activate it, the shell prompt is changed.

antkong@konga-mbp ~/dev/my-project (git-branch-name)
$ source .venv/bin/activate
(.venv) konga-mbp:my-project antkong$

How can I keep the prompts the same?

2
  • You may want to have a look at your activate file, specifically the PS1 variable, as described in this Unix & Linux SE question. You should (theoretically) be able to simply remove the offending prepended text. Commented Jan 31, 2020 at 2:18
  • 2
    I don't want to modify a script every time I recreate a virtual environment. There must be a simpler way. Commented Jan 24, 2021 at 21:32

1 Answer 1

8

The prompt of the Bash shell is controlled by the PS1 variable.

The activate script — near the bottom — keeps its old value in the _OLD_VIRTUAL_PS1 variable before prepending it with the venv's name:

if [ -z "${VIRTUAL_ENV_DISABLE_PROMPT:-}" ] ; then
    _OLD_VIRTUAL_PS1="${PS1:-}"
    if [ "x(gearshift3.8) " != x ] ; then
    PS1="(gearshift3.8) ${PS1:-}"
...

Therefore, to immediately revert back to your old PS1, type:

export PS1="$_OLD_VIRTUAL_PS1"

You may edit the activate scripts and disable the above conditional block, for all future venv activations, by replacing its 1st line with this:

if false; then

If you want to disable the prompt for all subsequent venv activations (during your shell session), set some value to the variable checked at the condition of the block:

export VIRTUAL_ENV_DISABLE_PROMPT=1

Finally, if you want this behavior to persist for all your future console sessions, add the above line into your ~/.bashrc.

1
  • I've exported VIRTUAL_ENV_DISABLE_PROMPT=1 at the top of my .bashrc. .zshrc, and even the direnv .envrc file for a specific python project root directory, and none of this is stopping the virtual environment prompt from spawning. I've even deleted the .direnv folder in the project root, then deleted all the bin/activate* scripts in it when it gets recreated, all to no avail. I'd love to be able disable it permanently at the system level, so I can then put it exactly where I want it in a two-line zsh left prompt, or my zsh right prompt if necessary, without duplicating venv prompts.
    – Inkayacu
    Commented Mar 29, 2021 at 16:54

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .