6

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?

Giacomo1968
  • 53,069
  • 19
  • 162
  • 212
Anthony Kong
  • 4,868
  • 11
  • 48
  • 80
  • You may want to have a look at your `activate` file, specifically the `PS1` variable, as described in this [Unix & Linux SE question](https://unix.stackexchange.com/questions/87062/how-to-display-the-name-of-the-current-virtualenv). You should (theoretically) be able to simply remove the offending prepended text. – Anaksunaman Jan 31 '20 at 02:18
  • 1
    I don't want to modify a script every time I recreate a virtual environment. There must be a simpler way. – Anthony Kong Jan 24 '21 at 21:32

1 Answers1

7

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.

Giacomo1968
  • 53,069
  • 19
  • 162
  • 212
ankostis
  • 205
  • 2
  • 10
  • 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 Mar 29 '21 at 16:54