Python debugger (pdb) stopped handlying up/down arrows, shows ^[[A instead
Asked Answered
R

3

53

I am using python 2.6 in a virtualenv on an Ubuntu Linux 11.04 (natty) machine. I have this code in my (django) python code:

import pdb ; pdb.set_trace()

in order to launch the python debugger (pdb).

Up until today, this worked fine. But now when the pdb starts, it works for debugging and running and breakpoints etc, but when I press the up arrow to show the previous command it prints ^[[A instead an doesn't go up. When I type something and press Home, it prints ^[OH instead of moving the cursor.

I can use up/home/etc. fine in the bash terminal which launches my python django unittests (which has the pdb call).

What's going on? How do I fix my pdb? What's wrong with my readline?

Redundancy answered 13/4, 2012 at 11:46 Comment(8)
@reos Yes, that was a typo. fixed now. it was …set_trace() in my codeRedundancy
did you try resetting your shell? resetSkiver
What if you "import readline" first? I'm not sure if this is really necessary, but can't hurt to try it.Mattson
Does the up arrow work in the Python interactive shell?Scandent
And can you try "import readline" from the PDB prompt?Scandent
@Rory, could you pls share how you solve it?Orest
FYI, I have the same problem but the cause of the debugger having trouble is that I set sys.stdout to Unbuffered so long running python apps I run with systemd actually log to journald. This causes the debugger to not work properly. I don't have a fix for it yet, other than rewriting all my print statements to call an unbuffered version of sys.stdout.Transmissible
I dont understand what the answers have to do with the question. It suddenly stopped working. Same in my case. Why suddenly?Photomicroscope
M
22

I found this problem exists when outputting to both console and file using python file.py 2>&1 | tee output.txt:

How to redirect stdout to both file and console with scripting?

After removing 2>&1 | tee output.txt, this problem (up arrow becomes ^[[A in pdb) disappear.

Midget answered 2/6, 2019 at 19:20 Comment(3)
Thx. Never expected this occurred the issue.Experientialism
Answers like these are why i love SO. I would have guessed that 2>&1 | tee was related but it was my issue as wellPew
In a similar manner, I'm debugging a library that implements a logger class and only experience the OP's issue after it executes the line sys.stdout = selfRemnant
P
21

Looks like from some reason you are missing the readline package. Simply install it by typing pip install readline and it should behave as expected. Remember to type it as superuser if you're not in venv.

Paulin answered 7/8, 2015 at 14:17 Comment(3)
It seems that the readline package is deprecated. It has been renamed to gnureadline to resolve a name clash with the standard library module.Casaubon
The current latest version of readline (6.2.4.1) is broken; it fails to pip install because the C code is invalid. Installing gnureadline produces no difference.Suburban
Doesn't work for me.Encode
T
1

See Python interactive mode history and arrow keys. In my case, Amadan's answer worked; I already had the readline module.

Timbuktu answered 12/1, 2013 at 19:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.