9

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_PROGRAMS\Languages\Python\3.6.1\lib\site-packages\pyreadline\console\console.py", line 768, in hook_wrapper_23
    res = ensure_str(readline_hook(prompt))
  File "C:\SP_CI_PROGRAMS\Languages\Python\3.6.1\lib\site-packages\pyreadline\rlmain.py", line 571, in readline
    self._readline_from_keyboard()
  File "C:\SP_CI_PROGRAMS\Languages\Python\3.6.1\lib\site-packages\pyreadline\rlmain.py", line 536, in _readline_from_keyboard
    if self._readline_from_keyboard_poll():
  File "C:\SP_CI_PROGRAMS\Languages\Python\3.6.1\lib\site-packages\pyreadline\rlmain.py", line 556, in _readline_from_keyboard_poll
    result = self.mode.process_keyevent(event.keyinfo)
  File "C:\SP_CI_PROGRAMS\Languages\Python\3.6.1\lib\site-packages\pyreadline\modes\emacs.py", line 243, in process_keyevent
    r = self.process_keyevent_queue[-1](keyinfo)
  File "C:\SP_CI_PROGRAMS\Languages\Python\3.6.1\lib\site-packages\pyreadline\modes\emacs.py", line 286, in _process_keyevent
    r = dispatch_func(keyinfo)
  File "C:\SP_CI_PROGRAMS\Languages\Python\3.6.1\lib\site-packages\pyreadline\modes\basemode.py", line 257, in complete
    completions = self._get_completions()
  File "C:\SP_CI_PROGRAMS\Languages\Python\3.6.1\lib\site-packages\pyreadline\modes\basemode.py", line 200, in _get_completions
    r = self.completer(ensure_unicode(text), i)
  File "C:\SP_CI_PROGRAMS\Languages\Python\3.6.1\Lib\rlcompleter.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_PROGRAMS\Languages\Python\3.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.

Mohsin Kale
  • 197
  • 2
  • 11

6 Answers6

13

Tested solution for Windows 10 (17 January 2020)

  • Copy last traceback file path C:\SP_CI_PROGRAMS\Languages\Python\3.6.1\Lib\rlcompleter.py
  • Open it with any text editor
    • If has VsCode use cmd and copy this
    • code C:\SP_CI_PROGRAMS\Languages\Python\3.6.1\Lib\rlcompleter.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

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
yEmreAk.com
  • 3,278
  • 2
  • 18
  • 37
  • 1
    Although this removes the error, it generates a weird behavior where an extra tab is added to the line. – Miguel A. Sanchez-Perez Nov 19 '19 at 21:16
  • Better is to comment out the whole `if not text.strip():` code block and let it suggest completion with all global vars – Phu Ngo Feb 22 '20 at 05:31
  • This got rid of the errors for me, but still didn't let me use the tab key (nothing would happen). This post fixed that issue: https://stackoverflow.com/questions/56838545/python-hitting-tab-fails-with-python-attributeerror-module-readline-has-no-at – Evan Rosica May 06 '20 at 10:16
  • 1
    I just commented out the line with `readline.redisplay()`, and set the line `readline.insert_text('\t')` to `readline.insert_text(' '*4)`. Like user2357112 says, this thing is completely abandoned (there are pull requests to fix this issue that won't get merged). There's probably a better readline solution for windows, but I can't be bothered to find it now (hence we're here hacking...) – Nathan Chappell May 18 '20 at 09:19
  • 4
    I love this edit history. – Eric B May 23 '22 at 23:27
9

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

Shakes
  • 531
  • 1
  • 5
  • 16
  • wont that throw an exception somewhere else – Mohsin Kale Jul 09 '18 at 17:31
  • @MohsinKale It seems like most users don't have any issues. It looks like it's called in the background by the python process in windows, so it throws an error whether you explicitly call it or not. You can always re-install if you do get an exception, but it seems like it's been abandoned – Shakes Jul 09 '18 at 17:38
  • 3
    How to uninstall / disable pyreadline? I'm encountering the same issue as OP on Windows / Python 3.7, pyreadline==2.1, but `pip uninstall pyreadline` results in `Cannot uninstall 'pyreadline'. It is a distutils installed project and thus we cannot accurately determine which files belong to it which would lead to only a partial uninstall.` This also did not work for me: https://stackoverflow.com/questions/51261584/unable-to-install-pyreadline-using-pip-on-windows – wkzhu Jul 01 '19 at 15:10
  • @wkzhu Apparently, the error you pasted occurs when the module was installed with another package manager. Are you using Anaconda (i.e. Conda)? If so try, `conda uninstall pyreadline`. Be careful though, as it may remove many modules that you need. I found that the answer below by @Yedhrab fixed the problem. Note that the path will be different, (copy it from the python error message) mine was: `C:\Users\\Anaconda3\lib\rlcompleter.py` – Evan Rosica May 06 '20 at 09:54
  • as at today, i found that installing manimgl, a version of the manim package for Mathematical Animation installed the pyreadline package as a dependency....some other such packages still have dependency to the pyreadline....am just pointing out! – Brian Obot Oct 16 '21 at 09:54
5

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.

user2357112
  • 260,549
  • 28
  • 431
  • 505
1

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.

0

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])
olive007
  • 760
  • 1
  • 12
  • 32
0

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