{"id":321,"library":"lxml","title":"lxml","description":"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.","status":"active","version":"6.0.2","language":"python","source_language":"en","source_url":"https://github.com/lxml/lxml","tags":["XML","HTML","XPath","XSLT","Parsing"],"install":[{"cmd":"pip install lxml","lang":"bash","label":"Install lxml"}],"dependencies":[{"reason":"Required for XML parsing capabilities.","package":"libxml2"},{"reason":"Required for XSLT transformations.","package":"libxslt"}],"imports":[{"note":"Ensure 'etree' is imported from 'lxml' for XML parsing.","symbol":"etree","correct":"from lxml import etree"},{"note":"Ensure 'html' is imported from 'lxml' for HTML parsing.","symbol":"html","correct":"from lxml import html"}],"quickstart":{"code":"import os\nfrom lxml import etree\n\n# Load XML from a file\nwith open(os.environ.get('XML_FILE_PATH', 'sample.xml'), 'rb') as f:\n    tree = etree.parse(f)\n\n# Perform XPath query\nresult = tree.xpath('//element[@attribute=\"value\"]')\n\n# Process result\nfor elem in result:\n    print(etree.tostring(elem))","lang":"python","description":"This script demonstrates loading an XML file and performing an XPath query using lxml's etree module."},"warnings":[{"fix":"Install 'lxml_html_clean' separately and update import statements accordingly.","message":"In version 5.2.0, the 'lxml.html.clean' module was moved to a separate project 'lxml_html_clean'.","severity":"breaking","affected_versions":"5.2.0"},{"fix":"Use 'result[0]' to access the first element of the XPath result list.","message":"XPath queries return lists; accessing the first element requires indexing.","severity":"gotcha","affected_versions":"all"},{"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.","message":"Script 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.","severity":"breaking","affected_versions":"all"},{"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.","message":"The 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.","severity":"breaking","affected_versions":"all"}],"env_vars":null,"last_verified":"2026-05-12T13:03:15.862Z","next_check":"2026-06-26T00:00:00.000Z","problems":[{"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.","cause":"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.","error":"ModuleNotFoundError: No module named 'lxml'"},{"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.","cause":"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.","error":"lxml.etree.XMLSyntaxError: Start tag expected, '<' not found"},{"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.","cause":"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.","error":"ImportError: DLL load failed: The specified module could not be found."},{"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.","cause":"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.","error":"ValueError: Unicode strings with encoding declaration are not supported. Please use bytes input or XML fragments without declaration."}],"ecosystem":"pypi","meta_description":null,"install_score":100,"install_tag":"verified","quickstart_score":0,"quickstart_tag":"stale","pypi_latest":null,"install_checks":{"last_tested":"2026-05-12","tag":"verified","tag_description":"installs cleanly on critical runtimes, fast import, recently tested","results":[{"runtime":"python:3.10-alpine","python_version":"3.10","os_libc":"alpine (musl)","variant":"default","exit_code":0,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":0.03,"mem_mb":2,"disk_size":"29.7M"},{"runtime":"python:3.10-slim","python_version":"3.10","os_libc":"slim (glibc)","variant":"default","exit_code":0,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":0.02,"mem_mb":2,"disk_size":"30M"},{"runtime":"python:3.11-alpine","python_version":"3.11","os_libc":"alpine (musl)","variant":"default","exit_code":0,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":0.07,"mem_mb":2.3,"disk_size":"31.5M"},{"runtime":"python:3.11-slim","python_version":"3.11","os_libc":"slim (glibc)","variant":"default","exit_code":0,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":0.05,"mem_mb":2.3,"disk_size":"32M"},{"runtime":"python:3.12-alpine","python_version":"3.12","os_libc":"alpine (musl)","variant":"default","exit_code":0,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":0.06,"mem_mb":2.4,"disk_size":"23.5M"},{"runtime":"python:3.12-slim","python_version":"3.12","os_libc":"slim (glibc)","variant":"default","exit_code":0,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":0.06,"mem_mb":2.4,"disk_size":"24M"},{"runtime":"python:3.13-alpine","python_version":"3.13","os_libc":"alpine (musl)","variant":"default","exit_code":0,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":0.05,"mem_mb":2.5,"disk_size":"23.2M"},{"runtime":"python:3.13-slim","python_version":"3.13","os_libc":"slim (glibc)","variant":"default","exit_code":0,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":0.05,"mem_mb":2.3,"disk_size":"24M"},{"runtime":"python:3.9-alpine","python_version":"3.9","os_libc":"alpine (musl)","variant":"default","exit_code":0,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":0.03,"mem_mb":1.9,"disk_size":"29.2M"},{"runtime":"python:3.9-slim","python_version":"3.9","os_libc":"slim (glibc)","variant":"default","exit_code":0,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":0.02,"mem_mb":1.9,"disk_size":"30M"}]},"quickstart_checks":{"last_tested":"2026-04-23","tag":"stale","tag_description":"widespread failures or data too old to trust","results":[{"runtime":"python:3.10-alpine","exit_code":1},{"runtime":"python:3.10-slim","exit_code":1},{"runtime":"python:3.11-alpine","exit_code":1},{"runtime":"python:3.11-slim","exit_code":1},{"runtime":"python:3.12-alpine","exit_code":1},{"runtime":"python:3.12-slim","exit_code":1},{"runtime":"python:3.13-alpine","exit_code":1},{"runtime":"python:3.13-slim","exit_code":1},{"runtime":"python:3.9-alpine","exit_code":1},{"runtime":"python:3.9-slim","exit_code":1}]}}