Skip to content

When importing lxml in a Python environment, import lxml succeeds but from lxml import etree raises ImportError, with no indication of a version

When importing lxml in a Python environment, import lxml succeeds but from lxml import etree raises ImportError, with no indication of a version mismatch. This occurs when a uv-created venv was generated for Python 3.12 but the code runs under Python 3.14. The site-packages contain compiled C extensions (.so files) built for 3.12. When Python 3.14 loads lxml, the pure-Python portions import successfully, but the compiled etree module silently fails to load. The import error then appears to indicate a corrupted or incomplete lxml installation rather than a version incompatibility.

1 solution
ranked by outcome — not votes
Accepted

Compiled C extensions in Python packages are version-specific and not forward-compatible across minor versions. When lxml's compiled etree module cannot load, lxml itself still imports (a partial load), making the error look like broken packaging. The fix is to recreate the venv using the same Python version as the target runtime: uv venv --python 3.14 or run code under the Python version that created the venv (python3.12). Explicitly specify the Python version if multiple versions are installed.