Home > Blockchain >  Python on Windows starts with `AttributeError: module 'collections' has no attribute '
Python on Windows starts with `AttributeError: module 'collections' has no attribute '

Time:10-12

Just installed python 3 on windows and started it on the terminal using the command "python". Then I had this error:

C:\Users\User>python
Python 3.10.8 (tags/v3.10.8:aaaf517, Oct 11 2022, 16:50:30) [MSC v.1933 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
Failed calling sys.__interactivehook__
Traceback (most recent call last):
  File "C:\Users\User\AppData\Local\Programs\Python\Python310\lib\site.py", line 446, in register_readline
    import readline
  File "C:\Users\User\AppData\Local\Programs\Python\Python310\lib\site-packages\readline.py", line 34, in <module>
    rl = Readline()
  File "C:\Users\User\AppData\Local\Programs\Python\Python310\lib\site-packages\pyreadline\rlmain.py", line 422, in __init__
    BaseReadline.__init__(self)
  File "C:\Users\User\AppData\Local\Programs\Python\Python310\lib\site-packages\pyreadline\rlmain.py", line 62, in __init__
    mode.init_editing_mode(None)
  File "C:\Users\User\AppData\Local\Programs\Python\Python310\lib\site-packages\pyreadline\modes\emacs.py", line 633, in init_editing_mode
    self._bind_key('space',       self.self_insert)
  File "C:\Users\User\AppData\Local\Programs\Python\Python310\lib\site-packages\pyreadline\modes\basemode.py", line 162, in _bind_key
    if not callable(func):
  File "C:\Users\User\AppData\Local\Programs\Python\Python310\lib\site-packages\pyreadline\py3k_compat.py", line 8, in callable
    return isinstance(x, collections.Callable)
AttributeError: module 'collections' has no attribute 'Callable'
>>>

(original screenshot)

I'm not understanding why I'm getting this error.

CodePudding user response:

try

BeautifulSoup AttributeError 'collections' has no attribute 'Callable'

Navigate to Python310\Lib\site-packages\pyreadline in your file browser.
Open py3k_compat.py in an text editor.
Change the 8th line from which should be return isinstance(x, collections.Callable) to return isinstance(x, collections.abc.Callable).
Save, exit and then retry.

or just reinstall python

CodePudding user response:

The issue is caused by the outdated package pyreadline present as a package importable by the Python installation. As this provides the readline module for Windows version of Python, the default site module will be able to import it and trigger the error. This outdated and no longer maintained package tries to import collections.abc.Callable from the deprecated location collections.Callable, it will fail under Python 3.10 as that deprecated import has been fully removed.

To solve this issue, just simply uninstall the pyreadline package, and should the features provided by readline is desirable, install the pyreadline3 package instead, which is currently maintained (as of 2022) and will provide the features expected from that package.

Please do ensure that pyreadline is first uninstalled before installing pyreadline3:

C:\Users\User>python -m pip uninstall pyreadline
Found existing installation: pyreadline 2.1
Uninstalling pyreadline-2.1:
  Would remove:
    c:\users\user\appdata\local\programs\python\python310\lib\site-packages\pyreadline-2.1-py3.10.egg-info
    c:\users\user\appdata\local\programs\python\python310\lib\site-packages\pyreadline\*
    c:\users\user\appdata\local\programs\python\python310\lib\site-packages\readline.py
Proceed (Y/n)? y
  Successfully uninstalled pyreadline-2.1

C:\Users\User>python -m pip install pyreadline3
Collecting pyreadline3
  Downloading pyreadline3-3.4.1-py3-none-any.whl (95 kB)
     ---------------------------------------- 95.2/95.2 kB ? eta 0:00:00
Installing collected packages: pyreadline3
Successfully installed pyreadline3-3.4.1

C:\Users\User>python
Python 3.10.8 (tags/v3.10.8:aaaf517, Oct 11 2022, 16:50:30) [MSC v.1933 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>>

Note that potential package conflicts may happen as both the pyreadline and pyreadline3 packages provide the readline module if both these packages are installed at the same time within the same Python environment/installation.

Alternatively, do as suggested by this comment which was posted under the question about BeautifulSoup, as there are other widely used Python packages that have not corrected this deprecated import in a timely manner.

  • Related