ImportError: DLL load failed: %1 is not a valid Win32 application
Fixes lxml failing to load on Windows with a Win32 application error at import time. Use when `from lxml import etree` raises `ImportError: DLL load failed: %1 is not a valid Win32 application`. Not for ModuleNotFoundError (lxml absent) or Linux/macOS import failures.
On Windows this means the lxml binary does not match your Python: a 32-bit wheel under 64-bit Python, or a corrupted download. Reinstall cleanly with pip uninstall lxml -y then pip install --only-binary :all: lxml, and make sure the Python bitness matches the wheel pip picked. The import works immediately after a matching wheel lands.
The error
ImportError: DLL load failed: %1 is not a valid Win32 applicationFix it
- Confirm your Python's bitness
python -c "import struct; print(struct.calcsize('P') * 8)"Expected: prints 64 on 64-bit Python or 32 on 32-bit Python. Write the number down.
- Reinstall lxml from a binary wheel only
pip uninstall lxml -y
pip install --only-binary :all: lxmlExpected: pip downloads a wheel whose filename contains win_amd64 for 64-bit Python or win32 for 32-bit Python, matching step 1.
- Confirm the import
python -c "from lxml import etree; print('ok')"Expected: prints ok with no DLL error.
When this applies
- the full text is
ImportError: DLL load failed: %1 is not a valid Win32 applicationonfrom lxml import etree - you are on Windows and recently switched Python versions or bitness
- the error appeared right after a pip install or upgrade of lxml
When it does NOT apply
- ModuleNotFoundError means lxml is not installed at all
DLL load failed: The specified module could not be foundis a different message (missing VC++ redist or dependency DLLs)- Linux/macOS
Symbol not founderrors are the Unix cousin, different fix
Compatibility
lxml 4.x/5.x on Windows, CPython 3.8+. The %1 phrasing is Windows-specific and comes from the OS loader.
Variant phrasings
ImportError: DLL load failed while importing etree: %1 is not a valid Win32 application
Newer Python versions name the module being imported (while importing etree). Same cause, same fix.
Why it happens
lxml ships compiled C extensions, and Windows is strict about binary compatibility. If pip installs a 32-bit (win32) wheel into a 64-bit Python, or a wheel file gets truncated mid-download, the OS loader refuses the DLL and Python reports %1 is not a valid Win32 application. The %1 is the loader's placeholder for the DLL path.
Edge cases
- If you must support 32-bit Python, keep it, but install the
win32wheel deliberately. Do not mix. - A stale
lxmldirectory left in site-packages by a manual copy can shadow the wheel. Uninstall first (step 2 does this). - If the error persists after a clean wheel install, check for conflicting
libxml2.dllcopies on PATH from other software. The wheel's bundled DLLs normally win, but a PATH entry can interfere.
Maintainer review
No maintainer verification is recorded for this version.
This records the version a maintainer checked. It does not assert that the version is the latest upstream release.