If I start a new Python interactive session from the command line, some console features such as using the arrow keys to access a previous command, etc. are present.
If instead, however, I use code.interact()
to start an interactive session from inside a larger script, the escape sequences aren’t properly handled – e.g. pressing the ⮹ key prints ^[[A
instead of displaying the previous command. How do I enable this feature?
Advertisement
Answer
You can use readline
module to get arrow keys working
JavaScript
x
11
11
1
import code
2
import readline
3
import rlcompleter
4
5
vars = globals()
6
vars.update(locals())
7
8
readline.set_completer(rlcompleter.Completer(vars).complete)
9
readline.parse_and_bind("tab: complete")
10
code.InteractiveConsole(vars).interact()
11