Registry / serialization / lxml
library6.1.2pypypi✓ verified 26d ago

lxml is a powerful and Pythonic XML processing library combining libxml2/libxslt with the ElementTree API. The current version is 6.0.2, released on March 28, 2026. It follows a regular release cadence, with recent versions 6.0.1 and 6.0.0 released on March 15, 2026, and March 1, 2026, respectively.

pip install lxml
INSTALL
IMPORT
SIG · LXML
L
lxml
serializationpythonv6.1.2
Install
2.2s avg
Import
43ms
Disk
28MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v6.1.2 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.95 runs
installs and imports cleanly · install 0.0s · import 0.046s · 29.7MB
glibc
py 3.103.95 runs
installs and imports cleanly · install 2.2s · import 0.040s · 30MB
28MB installed
● package 28MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

etree
from lxml import etree
Ensure 'etree' is imported from 'lxml' for XML parsing.
html
from lxml import html
Ensure 'html' is imported from 'lxml' for HTML parsing.

This script demonstrates loading an XML file and performing an XPath query using lxml's etree module.

import os from lxml import etree # Load XML from a file with open(os.environ.get('XML_FILE_PATH', 'sample.xml'), 'rb') as f: tree = etree.parse(f) # Perform XPath query result = tree.xpath('//element[@attribute="value"]') # Process result for elem in result: print(etree.tostring(elem))
Debug
Known issues
breakingIn version 5.2.0, the 'lxml.html.clean' module was moved to a separate project 'lxml_html_clean'.
fix
Install 'lxml_html_clean' separately and update import statements accordingly.
affects: 5.2.0
gotchaXPath queries return lists; accessing the first element requires indexing.
fix
Use 'result[0]' to access the first element of the XPath result list.
affects: all
breakingScript failed due to `FileNotFoundError`, indicating that the required input file 'sample.xml' (or the path specified by 'XML_FILE_PATH') was not found in the execution environment.
fix
Ensure the 'sample.xml' file is present in the working directory, or set the 'XML_FILE_PATH' environment variable to a valid existing file path before running the script.
affects: all
breakingThe script failed with a FileNotFoundError because 'sample.xml' (or the file specified by the XML_FILE_PATH environment variable) could not be found. This typically means the necessary input file is not present in the execution environment.
fix
Ensure that the 'sample.xml' file is present in the working directory or that the `XML_FILE_PATH` environment variable is correctly set to an existing file path within the execution environment.
affects: all
Errors
Common errors & fixes
ModuleNotFoundError: No module named 'lxml'
The 'lxml' library is not installed in the Python environment you are currently using, or there's a mismatch between the Python interpreter running the code and where 'lxml' was installed.
fix
Install the lxml package using pip: `pip install lxml` or `pip3 install lxml`. If using a virtual environment, ensure it's activated before installation.
lxml.etree.XMLSyntaxError: Start tag expected, '<' not found
This error typically occurs when attempting to parse a document that is not well-formed XML, or when trying to parse HTML content using `lxml.etree.fromstring()` instead of the dedicated HTML parser.
fix
If parsing HTML, use `lxml.html.fromstring()` instead of `lxml.etree.fromstring()`. Ensure the input document is valid XML if using the XML parser. Handle malformed documents by using an HTML parser or enabling recovery options where applicable.
ImportError: DLL load failed: The specified module could not be found.
This error usually occurs on Windows systems when the underlying C libraries (libxml2 and libxslt) that lxml depends on cannot be found by Python, often due to missing Visual C++ Redistributables or issues with how lxml was built/installed.
fix
Try reinstalling lxml (`pip uninstall lxml` then `pip install lxml`). If the issue persists, ensure you have the correct Microsoft Visual C++ Redistributable for your Python version installed, or try installing a pre-compiled wheel from unofficial sources if binary wheels are not available on PyPI for your Python/OS combination.
ValueError: Unicode strings with encoding declaration are not supported. Please use bytes input or XML fragments without declaration.
This error occurs when you provide a Python Unicode string (str type in Python 3) to `lxml.etree.fromstring()` that also contains an XML declaration specifying an encoding (e.g., `<?xml version="1.0" encoding="utf-8"?>`). The parser expects bytes for such declarations, or a plain Unicode string without the declaration.
fix
Either convert the string to bytes with the specified encoding before parsing (e.g., `xml_string.encode('utf-8')`), or remove the XML encoding declaration from the string if you intend to parse it as a plain Unicode string.
Upgrade
Version history
6.1.2latest on PyPI · released Aug 19, 2026
Audit
Dependencies
libxml2requiredRequired for XML parsing capabilities.
libxsltrequiredRequired for XSLT transformations.
Agent activity
8 hits · last 30 days
node
6
Resources