51

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?

Amandasaurus
  • 58,203
  • 71
  • 188
  • 248
  • @reos Yes, that was a typo. fixed now. it was ``…set_trace()`` in my code – Amandasaurus Apr 13 '12 at 11:49
  • 1
    did you try resetting your shell? `reset` – dm03514 Apr 13 '12 at 13:09
  • What if you "import readline" first? I'm not sure if this is really necessary, but can't hurt to try it. – FatalError Apr 13 '12 at 14:16
  • Does the up arrow work in the Python interactive shell? – Niels Bom Apr 17 '12 at 13:55
  • And can you try "import readline" from the PDB prompt? – Niels Bom Apr 17 '12 at 13:55
  • @Rory, could you pls share how you solve it? – K.Wanter Dec 31 '17 at 01:46
  • 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. – dubmojo Oct 09 '19 at 14:54
  • I dont understand what the answers have to do with the question. It suddenly stopped working. Same in my case. Why suddenly? – KansaiRobot Jul 11 '22 at 11:03

3 Answers3

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.

Tomasz Szymański
  • 1,445
  • 13
  • 17
  • 14
    It seems that the [_readline_](https://pypi.python.org/pypi/readline) package is deprecated. It has been renamed to [_gnureadline_](https://pypi.python.org/pypi/gnureadline) to resolve a name clash with the standard library module. – Yoel Feb 02 '18 at 11:49
  • 3
    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. – weberc2 May 07 '20 at 20:44
  • 3
    Doesn't work for me. – Kunyu Shi May 11 '22 at 05:15
20

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.

Xueqing Liu
  • 372
  • 4
  • 8
1

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

Community
  • 1
  • 1
user1880946
  • 101
  • 1
  • 4