24

I followed all steps to install Beautiful Soup, but it still comes out with this error:

AttributeError: module 'collections' has no attribute 'Callable'

Stacktrace

I am using Python 3.10.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Tan20211010
  • 305
  • 1
  • 2
  • 3
  • 7
    Please, check [ask]. Don't post images of code, error, etc. Copy/paste as text. Provide [mre] of your code as well as any relevant info like python version. – buran Oct 10 '21 at 12:24
  • 8
    It looks like BeautifulSoup does not support python3.10 yet. (In 3.10 the deprecated aliases to Collections Abstract Base Classes from the collections module were [removed](https://docs.python.org/3/whatsnew/3.10.html#removed)). . Downgrade your python version – buran Oct 10 '21 at 12:30
  • i sort of understand the downvotes, but it's a good question. though not a pure programming question it's about programming environment so on-topic and the fix is not obvious. if there's no duplicate we need it. and i'd gladly upvote @buran's answer. i've proposed an edit – diggusbickus Oct 10 '21 at 13:46
  • This *should* just work without issue on BeautifulSoup 4.6.1 or up (a release that was already 3 years old when this question was posted). – user2357112 Oct 13 '22 at 23:32
  • Please review *[Why not upload images of code/errors when asking a question?](https://meta.stackoverflow.com/questions/285551/)* (e.g., *"Images should only be used to illustrate problems that* ***can't be made clear in any other way,*** *such as to provide screenshots of a user interface."*) and take the appropriate [action](https://stackoverflow.com/posts/69515086/edit) (it covers command-line input/output as well). Thanks in advance. – Peter Mortensen Oct 21 '22 at 15:28

10 Answers10

45

collections.Callable has been moved to collections.abc.Callable in python 3.10+. A hacky solution is to add the reference back to collections before importing the problem library.

import collections
collections.Callable = collections.abc.Callable

from bs4 import BeautifulSoup # for example
Pranav Hosangadi
  • 23,755
  • 7
  • 44
  • 70
alinajafi
  • 594
  • 1
  • 5
  • 6
22

This solution worked for me:

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

I got the same AttributeError.

Then I looked inside the Python libcollections folder. It has ABCs (abstract base classes) made separately in version 3.3 and above.

So resolve the error by opening /bs4/elements.py in an editor and replace collections.callable with collections.abc.callable. Hence the callable attribute can be easily accessed.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
hitesh
  • 71
  • 2
  • 3
  • 1
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Dec 17 '21 at 10:46
4

Change "%LOCALAPPDATA%\Programs\Python\Python310\Lib\site-ackages\pyreadline\py3k_compat.py" - 8th code as below: return isinstance(x, collections.abc.Callable)

evandrix
  • 6,041
  • 4
  • 27
  • 38
YC Chan
  • 51
  • 1
  • 1
    As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jun 10 '22 at 15:49
  • Re *"8th code"*: Do you mean *"8th line"*? (*"8th code line"*)? Please respond by [editing (changing) your question/answer](https://stackoverflow.com/posts/72569342/edit), not here in comments (******** ***without*** ******** "Edit:", "Update:", or similar - the answer should appear as if it was written today). – Peter Mortensen Oct 21 '22 at 15:47
  • There is dwindling hope: *"Last seen more than a month ago"* – Peter Mortensen Oct 21 '22 at 15:49
3

You can uninstall the Python package pyreadline and install pyreadline3, which is an updated fork !

mxrch
  • 53
  • 6
  • This worked for me, today 2023-04-13, when getting this same error trying to use pdbpp. Just "pip uninstall pyreadline" then "pip install pyreadline3". Thank you! – khanfx Apr 13 '23 at 15:45
2

Beautiful Soup did not work with 3.10.

Reinstall with 3.9.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Shoikan
  • 47
  • 1
0

I was getting the same error while trying to run manticore, but apparently none of the answers were answering Mac users. So if you are on Mac and getting the same error, you have got to navigate to

lib/python3.10/site-packages/wasm/types.py Line 246

and change

isinstance(cur_field, collections.Callable) to isinstance(cur_field, collections.abc.Callable)

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Muhammad Hassan
  • 424
  • 3
  • 5
  • Why is it different? Due to using a different installer (not a rhetorical question)? [Homebrew](https://en.wikipedia.org/wiki/Homebrew_(package_manager))? Could it just be due to different Python versions (not a rhetorical question)? – Peter Mortensen Oct 21 '22 at 15:53
0

I had Beautiful Soup in a folder within the same folder as the Python program, as opposed to "installing" it.

So in that folder, I opened element.py and replaced all collections.Callable with collections.abc.Callable.

Python version 3.10.7

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
0

This error message indicates you have an old version of Beautiful Soup. The issue was already fixed back in Beautiful Soup 4.6.1, a release that came out in 2018. As of this writing, Beautiful Soup is up to version 4.11.1.

Update your Beautiful Soup, and the problem should be resolved.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
user2357112
  • 260,549
  • 28
  • 431
  • 505
0

instead of installing the zip folder of bs4, install it using your command prompt by typing: - pip install beautifulsoup4

  • 1
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jul 24 '23 at 15:04