7

How do you define a custom prompt to use when activating a Python virtual environment?

I have a bash script for activating a virtualenv I use when calling specific Fabric commands. I want the shell prompt to say something like "(fab)" so I can easily distinguish it from other shells I have open. Following this example, I've tried:

#!/bin/bash
script_dir=`dirname $0`
cd $script_dir
/bin/bash -c ". .env/bin/activate; PS1='(fab) '; exec /bin/bash -i"

but there's no change to the prompt. What am I doing wrong?


1
  • are you sourcing this script or running as child process ?
    – Arovit
    Dec 11, 2013 at 16:42

4 Answers 4

5

The prompt is set in the virtualenv's activate script (located in the bin folder under the virtualenv). If you only want to change the prompt some times, you could set an environment variable before calling activate (make sure to clear it in the corresponding deactivate file). If you simply want the prompt to be different all the time, you can do that right in activate at the line that looks like

set "PROMPT=(virtualenvname) %PROMPT%"

If you're using virtualenvwrapper, you could do all of this in the postactivate and postdeactivate scripts as well.

4
  • Your instructions are a little confusing. I only want to do this when called from the sample shell script I posted. How would I incorporate this into my script? Simply adding PROMPT='(fab)' like you suggest does not work.
    – Cerin
    Dec 11, 2013 at 17:04
  • You would need to set an environment variable in your shell script, then modify the activate script to set a different prompt if it your environment variable exists. If you look in the activate script, there are already sections with if/ else logic for environment variables. Just make sure to clear the variable in deactivate or you'll wind up always hitting the if case.
    – Tom
    Dec 11, 2013 at 17:07
  • @Tom, The activate script just looks for PS1, which I'm already defining. What environment variable are you referring to? How would I define it?
    – Cerin
    Dec 11, 2013 at 18:50
  • You'd set the variable in the bash script you mention like this and then look for the presence of the variable in the activate script.
    – Tom
    Dec 12, 2013 at 14:51
0

I couldn't find any way to do this via a script executed as a child process. Calling a separate bash process seems to forget any previously set PS1. However, it turned out to be trivial if I just sourced the script:

#!/bin/bash
script_dir=`dirname $0`
cd $script_dir
. .env/bin/activate
PS1="(fab) "
0

It appears the

exec /bin/bash -i

is resetting the PS1 variable. When I run

export PS1="foo "; bash

it resets it too. Curiously, when I look into the bash sources (shell.c and variables.c) it appears to use

set_if_not ("PS1", primary_prompt);

to init it. But I'm not exactly sure what happens between this and main(). Giving up.

0

I tried on and on (RedHat CentOS) as well. I found solution for both.

CYGWIN

After some investigation I found that the problem is that PS1 is set by /etc/bash.bashrc which overrides the PS1 env.var. So You need to disable to run this file using:

/bin/bash -c ". .env/bin/activate; PS1='(fab) ' exec /bin/bash -i --norc"

or

/bin/bash -c ". .env/bin/activate; export PS1='(fab) '; exec /bin/bash -i --norc"

LINUX

It works much simpler:

/bin/bash -c ". .env/bin/activate; PS1='(fab) ' exec /bin/bash -i"

or

/bin/bash -c ". .env/bin/activate; export PS1='(fab) '; exec /bin/bash -i"

If the script You are calling does not export the variables (and I suppose it does not) and the set variables does not appears in the environment then You could try something like this:

/bin/bash -c "PS1='(fab) ' exec /bin/bash --rcfile .env/bin/activate; "

I hope I could help!

6
  • None of these do anything for me. The last one even throws the error "/bin/bash: --: invalid option"
    – Cerin
    Dec 12, 2013 at 19:27
  • Cerin: Which version of bash you use? And on which platform?
    – TrueY
    Dec 12, 2013 at 23:40
  • 4.2.25 on Ubuntu 12.04
    – Cerin
    Dec 13, 2013 at 3:50
  • @Cerin: Sorry! I did not realize that '--rcfile' somehow does not like if mixed with short options! Remove '-i' before '--rcfile' in the last bash line.
    – TrueY
    Dec 13, 2013 at 12:36
  • That fixes the error, but it still doesn't show the custom PS1 value.
    – Cerin
    Dec 13, 2013 at 20:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.