Solving DLL load failed error when importing cv2 in Python

DLL load failed error when importing cv2 is a Python error which occurs when you try to import cv2 while using opencv.

In this article we will solve this issue and understand where it comes from. We will also include other possible solutions to the cv2 import problem.

Describing DLL load failed error when importing cv2 in Python

The error happens when you try importing cv2 after installing opencv on your machine.

Usually a line of code like this one will cause the error.

                                                                       #
from cv2 import cv2
                                                                       #

The error will usually look like this.

                                                                       #
ImportError: DLL load failed: The specified module could not be found.
                                                                       #

Bellow are the solutions which worked for me and will help you to successfully import cv2.

Solution 1 : Make a python 3 virtual environment

The first solution is to Make a python3 virtual environment, you can do that by using the code bellow

                                                                       #
virtualenv env_name --python="python3"
                                                                       #

Then you can run the following command

                                                                       #
pip3 install opencv-python
                                                                       #

After this, your can easily import cv2 with no issues whatsoever.

Solution 2 : install opencv from the command line

Sometimes, the error happens when you are running pip from the python terminal which is not the correct way to do this.

                                                                       #
pip install opencv-python
                                                                       #

You should run the command above outside of the Python terminal.

Also you should understand that cv2 only works for Python 3, fore example the import should fail in Python 2.

Solution 3 :

Another solution is to download the latest version of OpenCv corresponding to your Python version.

You can find it online, preferably on the official website.

Then make sure to run the following command

                                                                       #
pip install opencv_python‑3.2.0‑cp36‑cp36m‑win_amd64.whl
                                                                       #

The command above works for windows 64, if you have windows 32 you should run the command bellow

                                                                       #
pip install opencv_python‑3.2.0‑cp36‑cp36m‑win32.whl
                                                                       #

The solutions above should be enough to solve the problem, if you like our effort make sure to consider donating to our Kofi account, there is a red button that you can use if you are feeling generous.

Summing-up

This error is solved guys, thank you for reading our article, if you have any other errors make sure to search in our site for a solution, so far we are providing solutions for errors in Python, cheers and keep coding.

If you want to learn more about Python, please check out the Python Documentation : https://docs.python.org/3/