YED
YED

Reputation: 11

Python prints escape keys while entering input when pressing the arrow keys on Terminal

I made a simple Python 3 script which takes an input from the user. But while entering the input, if I press left arrow key, instead of going left it prints ^[[D . It happens with all arrow keys. But it doesn't happen in Terminal or Python Interactive Shell, it only happens when I run a Python script from Terminal and need to enter an input.

I use Ubuntu 19.10 and Anaconda distrubition which runs Python 3.7.

operation = input("Enter the expression: ")

How can I fix this?

Upvotes: 1

Views: 1265

Answers (1)

JBGreen
JBGreen

Reputation: 544

Import the readline package before using input

import readline
operation = input("Enter the expression: ")

https://docs.python.org/3/library/readline.html

Settings made using this module affect the behaviour of both the interpreter’s interactive prompt and the prompts offered by the built-in input() function.

Importing it will be enough to activate input line editing (arrow keys will move the cursor around instead of printing ^[[D, etc.). Other functions in the readline module can be used to set up tab completion and a history file.

Upvotes: 4

Related Questions