ImportError: DLL load failed: %1 is not a valid Win32 application

avatar
Borislav Hadzhiev

Last updated: Apr 13, 2024
4 min

banner

# ImportError: DLL load failed: %1 is not a valid Win32 application

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.

shell
pip install --upgrade pywin32

upgrade pywin32 to the latest version

The command will upgrade your pywin32 module to the latest version.

If you use Anaconda, you can also try to issue the following command.

shell
conda install --force-reinstall pywin32

force reinstall pywin32 using conda

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.

shell
pip install -- pywin32==227

# Install the module that caused the problem from the unofficial Windows Binaries

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.

  1. Open the following page.

  2. 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.

check python version and if 32 bit or 64 bit

You can also check if the Python interpreter is 32-bit or 64-bit by issuing the following command.

main.py
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.

check if python is 32 bit or 64 bit

  1. Click on the https://www.lfd.uci.edu/~gohlke/pythonlibs/ link and press Ctr + f to search for the package that caused the issue. I'll use opencv (cv2) for the example

Download the corresponding .whl file. For example, I would download the following file.

shell
opencv_python‑4.5.5‑cp310‑cp310‑win_amd64.whl
The cp310 part is the version (Python 3.10) and amd64 means 64-bit.

download correct package

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:

shell
pip install opencv_python‑4.5.5‑cp310‑cp310‑win_amd64.whl

issue pip install package command

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.

# Check where the module is coming from

If the error persists, use the importlib module to check where the offending module is located.

main.py
import importlib.util print(importlib.util.find_spec('cv2'))

check where offending module is located

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.

# Try to rerun the installation command with the --force-reinstall flag

If you got the error when using a specific package, try to reinstall the package with the --force-reinstall flag.

shell
pip install opencv-python --force-reinstall # Or with pip3 pip3 install opencv-python --force-reinstall

install package with force reinstall flag

Make sure to replace opencv-python with the name of the package that caused the error in your case.

# Having multiple versions of Python

You might also get the error if you've installed multiple Python versions on Windows.

If the error persists, you might want to:

  1. Make sure that the Python versions you've installed are not required by any underlying application (e.g. your Python installation is not required by the OS).
  2. Uninstall the Python installations that are not needed.
  3. If needed, install a 64-bit Python from the python.org website.

install 64 bit python from official site

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.