Last updated: Apr 13, 2024
Reading time·4 min
The Python error "ImportError: DLL load failed: %1 is not a valid Win32
application" most commonly occurs when you have an outdated version of pywin32
or are trying to install 32-bit modules on a 64-bit machine on Windows.
The first thing you should try is to open your terminal and issue the
pip install --upgrade pywin32
command.
pip install --upgrade pywin32
The command will upgrade your pywin32
module to the latest version.
If you use Anaconda, you can also try to issue the following command.
conda install --force-reinstall pywin32
Check if the error persists after having updated pywin32
to the latest
version.
If you use a very old version of Python, you can also try to pin your pywin32
version to 227
but in most cases, this is not necessary.
pip install -- pywin32==227
The error also often occurs when installing and using a specific package, e.g.
opencv-python
(cv2
).
You might run into issues if you've installed a 32-bit package on a 64-bit machine or vice versa.
The way to solve the error is to download and install the package that caused the issue from the unofficial Windows binaries for Python.
Open the following page.
Get your Python version and check if your Python installation is 64-bit or 32-bit.
You can do this by opening CMD
and typing python
or py
.
You can also check if the Python interpreter is 32-bit or 64-bit by issuing the following command.
python -c "import sys; print(sys.maxsize > 2**32)" # Or using the py alias py -c "import sys; print(sys.maxsize > 2**32)"
If the command returns True
, Python is 64-bit. If it returns False
, Python
is 32-bit.
Ctr
+ f
to search for the package that caused the issue.
I'll use opencv
(cv2
) for the exampleDownload the corresponding .whl
file. For example, I would download the
following file.
opencv_python‑4.5.5‑cp310‑cp310‑win_amd64.whl
cp310
part is the version (Python 3.10) and amd64
means 64-bit.Make sure your Python version matches the cp3XX
label and the package is
amd64
if you're running 64-bit
Python and win32
if you're running 32-bit
Python.
Once you download the file, open your terminal in the same directory as the
downloaded file (e.g. C:\Users\Example\Downloads
) and install it using pip
:
pip install opencv_python‑4.5.5‑cp310‑cp310‑win_amd64.whl
Make sure to update the command by replacing the name of the package with your specific package.
You have to make sure to only install 64-bit packages if your Python interpreter is running 64-bit Python.
If you are using a 32-bit Python and trying to install a 64-bit package, you'd run into errors and vice versa.
The most common cause of the error is having installed a 64-bit version of a package when using a 32-bit version of Python or vice versa.
If the error persists, use the importlib
module to check where the offending
module is located.
import importlib.util print(importlib.util.find_spec('cv2'))
The error is often caused when a module is being pulled from the Anaconda DLL directory.
If the command points to a file that is located in the Anaconda DLL directory
(e.g. cv2.pyd
), try to delete it.
After deleting the file, try to rerun the importlib
script and check where the
module is being pulled from.
--force-reinstall
flagIf you got the error when using a specific package, try to reinstall the package with the --force-reinstall flag.
pip install opencv-python --force-reinstall # Or with pip3 pip3 install opencv-python --force-reinstall
Make sure to replace opencv-python
with the name of the package that caused
the error in your case.
You might also get the error if you've installed multiple Python versions on Windows.
If the error persists, you might want to:
You can learn more about the related topics by checking out the following tutorials: