Neo Anderson
Neo Anderson

Reputation: 648

"DLL load failed" when import cv2 (opencv)

In [windows server 2012 R2 x64, python 3.7 64x]

pip install opencv-contrib-python

installed without any error .
and when I try to import it

import cv2

show me this error :

Traceback (most recent call last):
  File "test.py", line 1, in <module>
    import cv2
  File "C:\Program Files\Python37\lib\site-packages\cv2\__init__.py", line 3, in
 <module>
    from .cv2 import *
ImportError: DLL load failed: The specified module could not be found.

and I see another topic about this error but their solution does not work for me :
- DLL load failed error when importing cv2
- Failure to import cv2 in python : DLL load failed
- and a lot of pages in here

some of them say about that problem because anaconda, and I don't have anaconda even. and many say that problem 32 or 64 version, I try both and disappointment. many about system variables...
what dll failed to load? cv2? why?

what i try till now:

UPDATE
NEXT 2 DAY

I try to manage missing dll base on dependency walker that mention in bellow post, download all dll and put them in system 32, its a good clue but not solve the problem.

DAY 4,5

my next step , trying compile OpenCV in my machine , install visual studio 2012 + cmake and base on document compile 2 version , 64x and 32 , compiling have a lot of problem itself (like missing some lib from c lang and need to install Visual C++ Redistributable for Visual Studio 2012 ...) and I resolve all of them ,then I have 2 success compiled version but both have the same problem with missing dll, the compile process is so slow and give 2 day from me. so if fail on this mission :/ I search for a new way...

DAY 6

my next step is to try to run OpenCV in my machine with docker ( I try but docker windows just install on windows 10 and windows server 2016) so I search for an alternative. I found Vagrant that work like docker but with VM (visual box) today my time spend for this articles, it's amazing, the concept of docker and vagrant is so Attractive, and I play with this like that I have new puzzle :) first error show on ... and limitless errors go on :

Day 7

Vagrant tests failed too because my windows server 2012 is a vb machine (visual server) and run a visual box in another visual box impossible! so I need new clues :

Day 8-9

try install vagrant with VMware , I download VMware, unfortunately this plugin is not free (79$) so I try to work with pythonanywhere.com that 'gives you access to machines with a full Python environment already installed.' in this free account, you can't openurl with urllib , and more limit... so I wanna try to install ubuntu in VMware my self... I read here results after work. it's Impossible with same reason vb (visual box)

Upvotes: 24

Views: 59444

Answers (20)

beeeZeee
beeeZeee

Reputation: 53

Answer as of May 2024. If you have Windows 11 and Python 3.12:

The solution that worked for me was to download this Windows Media Feature pack.

Make sure to download the Media Feature pack for your Windows OS version (mine is Windows 11 N). Also, make sure to restart your computer for the installation to complete.

Upvotes: 0

anand_v.singh
anand_v.singh

Reputation: 2838

This can happen if you are using windows 10 N distribution, the N distributions does not come pre installed with windows media feature pack, which is required after OpenCV version 3.4 and onwards.

The preferred solution is to install the feature pack at : https://www.microsoft.com/en-us/software-download/mediafeaturepack

Be careful to choose the version that works with your current version of windows.

If that is not an option, fall back to an earlier version of OpenCV that does not have dll dependencies, you can do that by:pip install opencv-python=3.3.0.9

Since windows rolled out it's N version this problem has been seen at many places, and has many impacts across the windows environment, the fastest way to identify if you have this problem is open youtube in Edge browser, if it says HTML5 media plugin not found, this is the problem.

Update May 2020: There is a new way to install media pack for Windows 10 N.

https://support.microsoft.com/en-us/help/4562569/media-feature-pack-for-windows-10-n-may-2020

Update 16APR2024 : This option is nolonger available for Windows 10/11 N 22H2 series. The following will not work: DISM /Online /Add-Capability /CapabilityName:Media.MediaFeaturePack~~~~0.0.1.0

Upvotes: 36

lam vu Nguyen
lam vu Nguyen

Reputation: 631

if anyone makes silly mistake as I have just done. I use python3.8 so cannot downgrade opencv by running pip install opencv-python==3.3.0.9 (I have not tried downgrading by whl)

to downgrade opencv by running pip install, you should use python3.6, and opencv 3.3.0.9 will solve DLL load failed

Upvotes: 0

corolla
corolla

Reputation: 5656

Came here from Windows 10N, upgraded to Windows 11. Had to install Media Feature Pack under Settings -> Apps -> Optional Features -> Add an optional feature.

Windows settings

Upvotes: 0

mshiffer
mshiffer

Reputation: 81

Just ran into this problem of cv2 importable from my conda environment but not through the "same" environment in jupyter. The error I was getting was that it couldn't load the dll.

Additionally, I could not get opencv installed through jupyter, even using:

import sys
!conda install --yes --prefix {sys.prefix} numpy

I checked my path using:

import os
os.path

in both my shell and jupyter. They were the same(!).

I was finally able to get things working by running the command:

conda install nb_conda

in my conda environment, as outlined here: https://github.com/udacity/P1_Facial_Keypoints/issues/13

This was eye opening as it created a new install of jupyter note books associated with my env instead of anaconda, with five(!) environments to choose from when creating a page. There were three that seemed to be associated with my conda environment. Interestingly, now it only shows the one named environment as an option when creatin a new notebook again in jupyter (the one associated with the env), but it successfully imports cv2.

For a good read on why this is happening with jupyter: https://jakevdp.github.io/blog/2017/12/05/installing-python-packages-from-jupyter/

Upvotes: 0

Mohammad Ganjkhani
Mohammad Ganjkhani

Reputation: 21

I was having this problem on Windows. I resolved this error by checking instructions here.

Q: Import fails on Windows: ImportError: DLL load failed: The specified module could not be found?

A: If the import fails on Windows, make sure you have Visual C++ redistributable 2015 installed. If you are using older Windows version than Windows 10 and latest system updates are not installed, Universal C Runtime might be also required.

Windows N and KN editions do not include Media Feature Pack which is required by OpenCV. If you are using Windows N or KN edition, please install also Windows Media Feature Pack

Upvotes: 2

Huzaifa Farooq
Huzaifa Farooq

Reputation: 116

All you need is python 3.6. I've been looking for solution for last 3 days and my problem was solved when i installed python 3.6.7. After installing python 3.6 you can simply run pip install opencv-python. Source: https://www.geeksforgeeks.org/setup-opencv-with-pycharm-environment/

Upvotes: 0

Vishnu
Vishnu

Reputation: 1

Installing Python version 3.6 and then installing opencv with the command: pip install opencv-python==3.3.0.9 resolved this issue for me

Upvotes: 0

luka_matkic
luka_matkic

Reputation: 191

I had the same problem on Windows Server 2012 R2 x64. I was creating executable file using PyInstaller and got error in runtime:

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

After installing "Visual C++ redistributable" 2015 and enabling "Media Foundation" feature my problem was resolved.

There is more informations in documentation: OpenCV Documentation

Upvotes: 2

Sandip Bantawa
Sandip Bantawa

Reputation: 2880

I was having this problem on Windows Server 2008R2 fresh install and took almost a day to resolve, as was trying with many hits and trials finally I found solution somewhere in internet (not stackoverflow)

  • Installed Windows Media Feature Pack for 2008R2 then installed Server Manager-> Features-> Add Features-> Desktop Experience
  • Server Manager-> Features-> Add Features-> Desktop Experience and Ink and Handwriting Services.

Besides check list includes

  • Visual C++ redistributable 2015
  • Universal C Runtime

Hopefully its will help save time

Upvotes: 0

Faizi
Faizi

Reputation: 460

I was having the same issue. I resolved this error by downgrading open cv.

pip install opencv-python==3.3.0.9

Upvotes: 1

sergemart
sergemart

Reputation: 11

It helps me pip install opencv-contrib-python Anaconda Prompt, python 3.7.1 cv2 4.1.1

Upvotes: 1

kaythi swetun
kaythi swetun

Reputation: 53

its worked well for me.* Answer is Need to put cv2.pyd file to your virtual environment. need to put under two folder of envs,

  • first is under DLLS folder and Second is under Lib/site-packages

To get cv2.pyd > download from this link https://sourceforge.net/projects/opencvlibrary/ and then extract the download file

You will get opencv folder, after that go inside opencv folder like Downloads\opencv\build\python\2.7 and go one deeper folder depend on your 32 or 64 window version

Copy cv2.pyd Important **** after copying cv2.pyd file to your envs, you need to rename cv2.pyd to _cv2.pyd

Upvotes: 0

Nell
Nell

Reputation: 21

After 15 days of brain storming, This solution worked for me. And I am sure that it will work for you too. I installed anaconda to use OpenCV 3.1.0. I followed following Steps:

1) I have installed anaconda-5.3 64-bit installer (614.3 MB) which uses python 3.7. You can download anaconda from link: https://www.anaconda.com/download/#windows

2) After installation of anaconda, open anaconda prompt by typing "anaconda prompt" on windows start button. Open with "Run as administrator".

3) You have to create new environment to install and use OpenCV module.Write following commands to create and activate new environment:
>conda create --name myNewEnv python=3.5.0
>activate myNewEnv
"myNewEnv" is the name of new enviroment.

4)Now you need to install prerequisite for OpenCV, which is numpy and then install opencv3. Start internet before executing following commands:
>conda install numpy
>conda install anaconda-client
>conda install --channel https://conda.anaconda.org/menpo opencv3

5)OpenCV3 has been installed. Now, verify installation by executing following commands:
(myNewEnv) C:\Users\Nilesh> python
>>>import cv2
>>>cv2.__version__

Follow steps given below, when you want to start OpenCV3 second time onwards:

1) Open "anaconda command promt" from start menu with "run as administrator" rights
2) Type command >activate your_new_environment_name
3) Assume that you want to run file located on desktop, for that write following command:
> cd C:\Users\Nilesh\Desktop
4) Now your working directory is Desktop.
5) Type following command to run any code (for example test.py).
> python test.py
Note: Here, python means version-3.

I hope this solution will work for you.

Upvotes: 2

Rhythm sharma
Rhythm sharma

Reputation: 118

i was suffering from the same problem "DLL load failed" after reading tons of answers and articles i got a solution.

i don't know this works for you or not but give it a try.

tools and versions i used: anaconda - 5.3.1, python - 3.7, win 10 (64 bit)

Steps i performed :

step1:i installed opencv 3.4.4 from here then extract into C drive (you can do wherever you want)

step2: copy cv2.pyd file from [C:\opencv\build\python\cv2\python-3.7] here to [C:\Users\"user-name"\Anaconda3\Lib\site-packages] here.

step3: run dependency walker to detect which dll files are missing, after running dependency walker in my case it showed two dll files are not loading, they are (1)OPENCV_WORLD344.DLL and (2)IESHIMS.DLL. i don't know about your situation but you can do this-> find files name using dependency walker then find their path and follow step4.

step4: open System property->Advanced->Environment variables, now edit path and add C:\opencv\build\x64\vc14\bin (in my case this was the place where OPENCV_WORLD344.DLL is stored) and after doing this, import cv2 in python.

If my answer does not work then add a comment.

Upvotes: 1

Omda asli
Omda asli

Reputation: 41

I had similar issue except that I'm using Anaconda3 and this code here solved it:

conda install -c anaconda py-OpenCV

Upvotes: 3

Biks
Biks

Reputation: 705

I have faced the similar issue in Windows Server 2012 r2. After lot of findings I found that mfplat.dll was missing which is related to Window Media Service.

Hence you have to manually install the features so that you can get dll related to window media service.

  1. Turn windows features on or off
  2. Skip the roles screen and directly go to Feature screen
  3. Select "Desktop Experience" under "User Interfaces and Infrastructure"

After this all required dll of opencv would be available.

Upvotes: 7

cuzi
cuzi

Reputation: 1087

I had the same error (although I compiled OpenCV myself), in my case there were some DLL dependencies missing. You can check that with the program Dependency Walker.

Download Dependency Walker and run it, and open the file cv2.pyd with Dependency Walker, it should be in C:\Program Files\Python37\lib\site-packages\cv2\ if the pip package installed correctly.

This program then scans for dependencies (which may take a few minutes) and it then shows missing DLL files in red.

Upvotes: 9

Vikhyat Agarwal
Vikhyat Agarwal

Reputation: 1843

Python 3.7 may not support some modules.

  • Try installing python 3.6.4
  • Using command prompt, "pip install opencv" or "pip install opencv-python"
  • Import cv2

Upvotes: 4

Rakshith G B
Rakshith G B

Reputation: 826

Can you try uninstalling opencv and installing it using a wheel file instead? Here is a website that has many versions of OpenCV compiled for windows, search for the one you need and simply install it with pip command.

So if you have Python 3.6 (64 Bit) and wish to install OpenCV 3.4.3 then download the wheel file: opencv_python‑3.4.3‑cp36‑cp36m‑win_amd64.whl

Upvotes: 3

Related Questions