AttributeError: module 'html5lib.treebuilders' has no attribute '_base'
Fixes the html5lib version-skew crash that surfaces while importing MechanicalSoup's dependency chain. Use when the traceback ends in `AttributeError: module 'html5lib.treebuilders' has no attribute '_base'`. Not for missing modules.
Your html5lib is too old (or too new) for the BeautifulSoup version in the chain: bs4 reaches for html5lib.treebuilders._base, which only exists in compatible html5lib releases. Upgrade html5lib with python -m pip install -U html5lib and the AttributeError goes away. If you pinned old versions deliberately, unpin them together.
The error
AttributeError: module 'html5lib.treebuilders' has no attribute '_base'Fix it
- Upgrade html5lib
python -m pip install -U html5libExpected: installs html5lib 1.1.
- Confirm the import chain
python -c "import mechanicalsoup; print('ok')"Expected: prints ok. bs4 finds the _base attribute it expects.
- If it persists, refresh the whole parsing stack together
python -m pip install -U beautifulsoup4 html5lib lxmlExpected: mutually compatible releases. Version skew between these three is the entire category of bug.
When this applies
- the AttributeError names
html5lib.treebuildersand_base - it fires during
import mechanicalsouporfrom bs4 import BeautifulSoupwith the html5lib parser - an old lockfile pins html5lib to a pre-1.0 release
When it does NOT apply
No module named 'html5lib'is the missing install, different fixNo module named 'bs4'deeper in the chain is the stub problem
Compatibility
html5lib 1.1 is the current release and the one bs4 expects. The _base module exists in html5lib 1.x; pre-1.0 layouts differ.
Why it happens
BeautifulSoup's html5lib tree builder imports html5lib's internal treebuilders._base module. html5lib 0.x organized its internals differently, so a bs4 new enough to expect _base paired with an html5lib old enough not to have it crashes with AttributeError at import. It is pure version skew, usually from a lockfile that pinned html5lib years ago while bs4 floated forward.
Edge cases
- If you do not use the html5lib parser anywhere, you can also dodge this by parsing with
lxmlorhtml.parserinstead of upgrading. But upgrading is cleaner. - Some distros ship patched html5lib. Prefer the PyPI release in your venv over the system package.