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 showFix it fast
Most common cause, 1 of 3lxml 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.
pip install --upgrade "lxml==6.1.3" "xmlsec==1.3.18"
# then update the same two pins in requirements.txt or uv.lockShort 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.
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.
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)
EOFThen find the line you see in this list and open its fix:
- 1lxml is pinned to a release the xmlsec wheel was not built for
Both tags are prebuilt wheels (manylinux, musllinux, macosx or win), but the lxml version differs from the one the xmlsec release notes name - 2One package was compiled here against the system libxml2
The xmlsec tag (or the lxml one) ends in plain linux_x86_64, while the other is a manylinux wheel - 3uWSGI loaded the system libxml2 before Python started
python -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.
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.
pip install --upgrade "lxml==6.1.3" "xmlsec==1.3.18"
# then update the same two pins in requirements.txt or uv.lockVerify
Both versions now match the pair from the release notes, and the import prints the same libxml2 tuple twice.
python -c "import lxml.etree, xmlsec; print(lxml.etree.LIBXML_VERSION, xmlsec.get_libxml_version())"Confirmed by github.com
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.
python -m pip config list; env | grep PIP_NO_BINARY; python -m pip cache list xmlsecFix
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.
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 xmlsecVerify
Run the first-step script again. Both packages now show the same kind of tag, and import xmlsec raises nothing.
python -c "import xmlsec; print(xmlsec.get_libxml_version(), xmlsec.get_libxml_compiled_version())"Confirmed by raw.githubusercontent.com
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.
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.
sudo apt-get install pkg-config libxml2-dev libxslt1-dev libxmlsec1-dev libxmlsec1-openssl
pip install --force-reinstall --no-binary=lxml,xmlsec lxml xmlsecVerify
Start uWSGI with your usual config file. The log no longer shows the InternalError or the no app loaded line.
uwsgi --ini uwsgi.iniConfirmed by github.com
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.