Pythons Console Module has made it impossible to type the tab key

Question:

I started using it on one of my programs a while back. Ever since, whenever I type the tab key on a console (cmd.exe instance) with python running, I get a readline internal error. Full traceback is as follows (note I haven’t imported the cmd module in this context or even imported a script using it. I’ve simply started python, pressed tab and voila an exception):

<pre>
Traceback (most recent call last):
  File "C:SP_CI_PROGRAMSLanguagesPython3.6.1libsite-packagespyreadlineconsoleconsole.py", line 768, in hook_wrapper_23
    res = ensure_str(readline_hook(prompt))
  File "C:SP_CI_PROGRAMSLanguagesPython3.6.1libsite-packagespyreadlinerlmain.py", line 571, in readline
    self._readline_from_keyboard()
  File "C:SP_CI_PROGRAMSLanguagesPython3.6.1libsite-packagespyreadlinerlmain.py", line 536, in _readline_from_keyboard
    if self._readline_from_keyboard_poll():
  File "C:SP_CI_PROGRAMSLanguagesPython3.6.1libsite-packagespyreadlinerlmain.py", line 556, in _readline_from_keyboard_poll
    result = self.mode.process_keyevent(event.keyinfo)
  File "C:SP_CI_PROGRAMSLanguagesPython3.6.1libsite-packagespyreadlinemodesemacs.py", line 243, in process_keyevent
    r = self.process_keyevent_queue[-1](keyinfo)
  File "C:SP_CI_PROGRAMSLanguagesPython3.6.1libsite-packagespyreadlinemodesemacs.py", line 286, in _process_keyevent
    r = dispatch_func(keyinfo)
  File "C:SP_CI_PROGRAMSLanguagesPython3.6.1libsite-packagespyreadlinemodesbasemode.py", line 257, in complete
    completions = self._get_completions()
  File "C:SP_CI_PROGRAMSLanguagesPython3.6.1libsite-packagespyreadlinemodesbasemode.py", line 200, in _get_completions
    r = self.completer(ensure_unicode(text), i)
  File "C:SP_CI_PROGRAMSLanguagesPython3.6.1Librlcompleter.py", line 80, in complete
    readline.redisplay()
AttributeError: module 'readline' has no attribute 'redisplay'
</pre>

Before u ask, I installed python to the directory “C:SP_CI_PROGRAMSLanguagesPython3.6.1”. It’s accessible from the path variable. Also any scripts I design I place in a directory u can also access from the path variable (including one using the cmd module for python).

This may not seem like a pressing concern, especially seeing as I can just type 4 spaces instead, however using tabs is something I’ve become especially accustomed to and the second I type the tab key, anything I’ve written in a previous block is immediately lost as the traceback is printed. Please, can someone tell me how to fix this.

Edit: This is only within the python interpreter. Typing tab within a running program or something else doesn’t pose any problems.

Asked By: Mohsin Kale

||

Answers:

Stop using pyreadline. It’s been abandoned. What you’re seeing is a known issue, but unless someone takes over pyreadline development, it’s unlikely to ever be fixed.

Answered By: user2357112

Seems to be a continuing issue for Windows machines as seen on Github. A workaround seems to be uninstalling the pyreadline package.

Answered By: Shakes

Tested solution for Windows 10 (17 January 2020)

  • Copy last traceback file path C:SP_CI_PROGRAMSLanguagesPython3.6.1Librlcompleter.py
  • Open it with any text editor
    • If has VsCode use cmd and copy this
    • code C:SP_CI_PROGRAMSLanguagesPython3.6.1Librlcompleter.py
  • Look the line 80 which traceback tell us
  • Change these line (starts 79) like bellow and It will works
  • There will no error message and unnecessary tab more
...
if _readline_available:                  ## The old one is ##
    if hasattr(readline, 'redisplay'):   # if _readline_available:
        readline.insert_text('t')       #     readline.insert_text('t')
        readline.redisplay()             #     readline.redisplay()
    return ''                            # return ''
...

Do not forget to relaunch your python terminal

Answered By: Yedhrab

pyreadline can be uninstalled by typing pip uninstall pyreadline in the command prompt. I was experiencing the same issue, but after uninstalling pyreadline, Tab key is working for me.

NOTE: To see the installed packages, type pip list in command prompt.

Answered By: PhysicistSouravDas

Well if you don’t have permission on the machine. You won’t be able to do those uninstall solutions.

So set the completer function. It will solve the problem and won’t have weird behaviors. But the auto-completion won’t work anymore. If you want it to work have a look at the documentation and implement it.

readline.set_completer(lambda t, s: [None][s])
Answered By: olive007

What has worked for me (while the others did not for some unbeknownst reason) is the following:

In line 78:

if _readline_available:
    if hasattr(readline, 'redisplay'):
        readline.insert_text('t')
        readline.redisplay()
    return ' ' * 4
Answered By: Sharon Magnezy
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.