ModuleNotFoundError: No module named 'lxml'
Fixes the plain missing-module error for lxml, the XML/HTML parsing library half the scraping stack depends on. Use when `import lxml` or `from lxml import etree` fails with ModuleNotFoundError. Not for DLL load failures on Windows or the lxml_html_clean split.
lxml is not installed in the Python running your code. Run python -m pip install lxml and the import works. On most systems pip fetches a prebuilt wheel, so there is nothing else to do. If pip tries to build from source and fails, you are on a platform without wheels and need the libxml2/libxslt development headers first.
The error
ModuleNotFoundError: No module named 'lxml'Fix it
- Install lxml with the running interpreter's pip
python -m pip install lxmlExpected: Successfully installed lxml-....
- Confirm the import
python -c "from lxml import etree; print(etree.LXML_VERSION)"Expected: prints a version tuple like (5, 2, 1, 0).
- If pip starts compiling and fails, add the system libraries (Linux)
sudo apt-get install libxml2-dev libxslt1-dev
python -m pip install lxmlExpected: the build now finds the headers and completes. Only needed where no wheel exists, such as Alpine/musl or unusual architectures.
When this applies
import lxml,from lxml import etree, orfrom lxml import htmlraises ModuleNotFoundError- a scraper dependency (pyquery, newspaper3k, ebook tooling) fails importing lxml
- a fresh venv, container, or CI runner
When it does NOT apply
ImportError: DLL load failedon Windows is a bitness mismatch, not a missing packageNo module named 'lxml_html_clean'is the lxml 5.x split, install that packagecannot import name 'etree' from 'lxml'means lxml imported but is broken or ancient
Compatibility
lxml 4.x and 5.x. Wheels cover CPython 3.8+ on Windows, macOS (Intel and ARM), and manylinux. Python 3.12+ needs lxml 4.9.3 or newer.
Why it happens
lxml is a C extension, so unlike pure-Python packages it cannot be vendored by accident; if it was never installed, the import fails outright. Scraping libraries treat it as an optional-but-assumed dependency, so the error often surfaces one layer up (in pyquery or BeautifulSoup with the lxml parser) instead of at your own import line.
Edge cases
- Do not
pip install xml. There is no such package that provides lxml. - If you are on Python 3.12+ with an old pinned lxml (4.9.2 or older), upgrade: old lxml wheels do not support 3.12 and old source builds fail.
- Corporate proxies sometimes block the wheel download and pip silently falls back to a source build that fails.
pip install --only-binary lxml lxmlforces the wheel or errors clearly.