Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python prompt with a bash like interface

Tags:

python

I am using the python prompt to practice some regular expressions. I was wondering if there was a way to use the up/down arrows (like bash) to cycle through the old commands typed. I know its possible since it works on python on cygwin/windows. thanks

like image 430
Pradyot Avatar asked Feb 25 '26 00:02

Pradyot


2 Answers

Use the rlcompleter module to get both readline and completion.

Sample PYTHONSTARTUP code:

try:
  import readline
except ImportError:
  print "Module readline unavailable."
else:
  import rlcompleter
  readline.parse_and_bind("tab: complete")

Sample .bashrc code to set your python startup file:

if [ -f ~/.pythonstartup.py ]
then
  export PYTHONSTARTUP=~/.pythonstartup.py
fi

As well as compiling with readline enabled as suggested in another answer, you can also use rlrwrap to add readline at run time, even if it wasn't complied in; like so:

rlwrap python
like image 33
Benji York Avatar answered Feb 27 '26 14:02

Benji York