Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to customize virtualenv shell prompt

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?


like image 573
Cerin Avatar asked Oct 03 '22 05:10

Cerin


1 Answers

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.

like image 158
Tom Avatar answered Oct 13 '22 09:10

Tom