Reputation: 1404
In linux, the arrow keys don't work when I try to enter data for an input()
function. I get escape characters. See below (when I pressed left arrow key).
dp@hp:~$ python3 -c "x = input('enter a number '); print(x)"
enter a number 123^[[D^[[D
I have readline
installed (I am able to import it in the python shell).
The arrow keys work fine in the interactive interpreter but not in the above case (or when I execute input()
from a script).
What could be the reason?
Upvotes: 7
Views: 5329
Reputation: 113
I use linux too and for any module you have to import it.
For example for the readline module I have to do
import readline
This applies to all modules even the os or sys module I have to do
import os
import sys
However this only applies to modules that you installed correctly. If you installed readline incorrectly not even
import readline
will work.
That means for you
python3 -c "import readline; x = input('enter a number '); print(x)"
if you are doing it straight from the console and this applies to not only readline but every other module that you have and will get.
Upvotes: 1
Reputation: 311606
The documentation says:
If the readline module was loaded, then input() will use it to provide elaborate line editing and history features.
In your example, you haven't loaded the readline
module. Compare the behavior of this:
x = input('enter a number:')
print(x)
To this:
import readline
x = input('enter a number:')
print(x)
The second example will behave as you expect (readline support is active, arrow keys work, etc) while the first example will not have readline support.
On the command line, this would be:
python3 -c "import readline; x=input('enter a number '); print(x)"
Upvotes: 19