Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4791 pages
Node.js, PHP and APIs

import xmlsec fails because lxml runs a different libxml2

Your error

xmlsec.InternalError: (-1, 'lxml & xmlsec libxml2 library version mismatch')1 more line it can show

Fix it fast

Most common cause, 1 of 3

lxml is pinned to a release the xmlsec wheel was not built for

Move lxml to the release named in the notes of your xmlsec version and pin the two together.

Fix
pip install --upgrade "lxml==6.1.3" "xmlsec==1.3.18"
# then update the same two pins in requirements.txt or uv.lock
Did this fix it?
Full steps for this cause: check, fix, verify

Short answer

xmlsec.InternalError (-1, 'lxml & xmlsec libxml2 library version mismatch') means lxml and xmlsec loaded libxml2 builds with different major.minor versions. Usually lxml is pinned to a release the xmlsec wheel was not built for, or one package was compiled locally. For xmlsec 1.3.18, install lxml 6.1.3 from wheels, or build both from source.

  • Applies to xmlsec 1.3.18 (wheels bundle libxml2 2.14.6), lxml 6.1.3, pip 26.3, uv 0.13.9, Python 3.10 to 3.15
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Which cause do you have?

Run this in the same virtual environment as the app. It prints the libxml2 version lxml is running on and the wheel tag of each package, without importing xmlsec. On Linux, a tag that ends in plain linux_x86_64 or linux_aarch64 (no manylinux or musllinux) was built on your machine.

Run this first
python - <<'EOF'
from importlib.metadata import distribution
import lxml.etree
print("lxml runs libxml2", lxml.etree.LIBXML_VERSION)
for name in ("lxml", "xmlsec"):
    dist = distribution(name)
    tags = [line for line in dist.read_text("WHEEL").splitlines() if line.startswith("Tag")]
    print(name, dist.version, tags)
EOF

Then find the line you see in this list and open its fix:

  1. 1lxml is pinned to a release the xmlsec wheel was not built forBoth tags are prebuilt wheels (manylinux, musllinux, macosx or win), but the lxml version differs from the one the xmlsec release notes name
  2. 2One package was compiled here against the system libxml2The xmlsec tag (or the lxml one) ends in plain linux_x86_64, while the other is a manylinux wheel
  3. 3uWSGI loaded the system libxml2 before Python startedpython -c "import xmlsec" works in the same venv, but the uWSGI log shows the error and no app loaded

What does the error look like?

  • xmlsec.InternalError: (-1, 'lxml & xmlsec libxml2 library version mismatch')
  • unable to load app 0 (mountpoint='') (callable not found or import error)

Cause 1 of 3

lxml is pinned to a release the xmlsec wheel was not built for

You seeBoth tags are prebuilt wheels (manylinux, musllinux, macosx or win), but the lxml version differs from the one the xmlsec release notes name

Check

The libxml2 version mismatch error appears when xmlsec's import check sees lxml on a libxml2 with a different major or minor number. The xmlsec 1.3.18 wheels are built on libxml2 2.14.6, the same copy that lxml 6.1.3 ships. An older lxml pin left in requirements.txt or a lock file is enough to break the pair.

Check
pip show lxml xmlsec | grep -E '^(Name|Version)'

Fix

Move lxml to the release named in the notes of your xmlsec version and pin the two together. Upgrading xmlsec alone does not help if the lock file still holds the old lxml.

Fix
pip install --upgrade "lxml==6.1.3" "xmlsec==1.3.18"
# then update the same two pins in requirements.txt or uv.lock

Verify

Both versions now match the pair from the release notes, and the import prints the same libxml2 tuple twice.

Verify
python -c "import lxml.etree, xmlsec; print(lxml.etree.LIBXML_VERSION, xmlsec.get_libxml_version())"

Confirmed by github.com

Did this fix it?

Cause 2 of 3

One package was compiled here against the system libxml2

You seeThe xmlsec tag (or the lxml one) ends in plain linux_x86_64, while the other is a manylinux wheel

Check

Here the mismatch comes from mixing build types: an lxml wheel carries its own libxml2, while an xmlsec compiled on the server links the libxml2 from the OS packages. The project README says not to mix the two. A source build happens when pip finds no wheel for your Python or platform, or when --no-binary is set in pip.conf or an environment variable.

Check
python -m pip config list; env | grep PIP_NO_BINARY; python -m pip cache list xmlsec

Fix

Remove both packages and reinstall them as wheels. pip keeps the wheels it built locally and can reuse them, so delete those from the cache first; uv users clear the uv cache instead. If your platform has no wheels, install the libxml2, libxslt and xmlsec1 development packages and build both from source so they share one libxml2.

Fix
pip uninstall -y lxml xmlsec
pip cache remove xmlsec
pip cache remove lxml
pip install --only-binary=lxml,xmlsec lxml xmlsec

# uv
uv pip uninstall lxml xmlsec
uv cache clean
uv pip install --only-binary lxml --only-binary xmlsec lxml xmlsec

# no wheels for this platform (Debian/Ubuntu): build both
sudo apt-get install pkg-config libxml2-dev libxslt1-dev libxmlsec1-dev libxmlsec1-openssl
pip install --no-binary=lxml,xmlsec lxml xmlsec

Verify

Run the first-step script again. Both packages now show the same kind of tag, and import xmlsec raises nothing.

Verify
python -c "import xmlsec; print(xmlsec.get_libxml_version(), xmlsec.get_libxml_compiled_version())"

Confirmed by raw.githubusercontent.com

Did this fix it?

Cause 3 of 3

uWSGI loaded the system libxml2 before Python started

You seepython -c "import xmlsec" works in the same venv, but the uWSGI log shows the error and no app loaded

Check

uWSGI can pull the operating system's libxml2 into the process before your app imports lxml or xmlsec, so a third libxml2 copy decides the check. The xmlsec maintainers list this as a known edge case; the same code passes from the plain Python interpreter.

Check
python -c "import xmlsec, lxml.etree; print('ok', lxml.etree.LIBXML_VERSION)"

Fix

Make uWSGI and the Python packages resolve to the same libxml2. Rebuilding lxml and xmlsec from source in that environment ties both to the system copy that uWSGI already loaded.

Fix
sudo apt-get install pkg-config libxml2-dev libxslt1-dev libxmlsec1-dev libxmlsec1-openssl
pip install --force-reinstall --no-binary=lxml,xmlsec lxml xmlsec

Verify

Start uWSGI with your usual config file. The log no longer shows the InternalError or the no app loaded line.

Verify
uwsgi --ini uwsgi.ini

Confirmed by github.com

Did this fix it?

How do I stop it coming back?

  • Upgrade lxml and xmlsec in one pull request, using the lxml version that the xmlsec release notes name for its wheels.
  • Add `python -c "import xmlsec"` as a build step in the Docker image, so a mismatch fails the build instead of the first SAML login.
  • Keep --no-binary out of global pip.conf; set it per package only when both lxml and xmlsec are built from source together.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine on the same stack: stegnor.com, Menulo, ShopGo.

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.