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

```text
ModuleNotFoundError: No module named 'lxml'
```

## Fix it

1. **Install lxml with the running interpreter's pip**

```
python -m pip install lxml
```
Expected: `Successfully installed lxml-...`.

2. **Confirm the import**

```
python -c "from lxml import etree; print(etree.LXML_VERSION)"
```
Expected: prints a version tuple like `(5, 2, 1, 0)`.

3. **If pip starts compiling and fails, add the system libraries (Linux)**

```
sudo apt-get install libxml2-dev libxslt1-dev
python -m pip install lxml
```
Expected: 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`, or `from lxml import html` raises 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 failed` on Windows is a bitness mismatch, not a missing package
- `No module named 'lxml_html_clean'` is the lxml 5.x split, install that package
- `cannot 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 lxml` forces the wheel or errors clearly.
