19

This question is different from "How do I install Python OpenCV through Conda?" because that question was asked more than 5 years ago, when all packages had different versions. I tried ALL answers to that question, and neither worked. See the text of question for details.

How to install opencv with conda now, in July 2019? On a freshly installed anaconda, I did conda update conda (succesfully) then tried the following:

(base) C:\Users\mlearning>python
Python 3.7.3 (default, Mar 27 2019, 17:13:21) [MSC v.1915 64 bit (AMD64)] :: Anaconda, Inc. on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import cv2
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'cv2'
>>> import cv
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'cv'

(base) C:\Users\mlearning>conda install -c menpo opencv3
Collecting package metadata (repodata.json): done
Solving environment: (goes into infinite loop, after 10 minutes I pressed ^C)

(base) C:\Users\mlearning>conda install opencv
Collecting package metadata (repodata.json): done
Solving environment: failed
Initial quick solve with frozen env failed.  Unfreezing env and trying again.
Solving environment: failed

UnsatisfiableError: The following specifications were found to be incompatible with each other:

Package zipp conflicts for:
importlib_metadata -> zipp[version='>=0.3.2']
path.py -> importlib_metadata[version='>=0.5'] -> zipp[version='>=0.3.2']
anaconda==2019.03=py37_0 -> importlib_metadata==0.8=py37_0 -> zipp[version='>=0.3.2']
zipp
Package importlib_metadata conflicts for:
anaconda==2019.03=py37_0 -> importlib_metadata==0.8=py37_0
path.py -> importlib_metadata[version='>=0.5']
Package hdf5 conflicts for:
anaconda==2019.03=py37_0 -> h5py==2.9.0=py37h5e291fa_0 -> hdf5[version='>=1.10.4,<1.10.5.0a0']
hdf5
opencv -> hdf5[version='>=1.10.2,<1.10.3.0a0,>=1.8.18,<1.8.19.0a0,>=1.8.20,<1.9.0a0']
h5py -> hdf5[version='>=1.10.1,<1.10.2.0a0,>=1.10.2,<1.10.3.0a0,>=1.10.4,<1.10.5.0a0,>=1.8.18,<1.9.0a0']
pytables -> hdf5[version='>=1.10.1,<1.10.2.0a0,>=1.8.18,<1.8.19.0a0,>=1.8.18,<1.9.0a0']
Package mkl-service conflicts for:
mkl-service
anaconda==2019.03=py37_0 -> mkl-service==1.1.2=py37hb782905_5

(base) C:\Users\mlearning>conda install -c conda-forge opencv
Collecting package metadata (repodata.json): done
Solving environment: failed
Initial quick solve with frozen env failed.  Unfreezing env and trying again.
Solving environment: failed

UnsatisfiableError: The following specifications were found to be incompatible with each other:

Package hdf5 conflicts for:
anaconda==2019.03=py37_0 -> hdf5==1.10.4=h7ebc959_0
h5py -> hdf5[version='1.10.1,1.8.17|1.8.17.*,1.8.18|1.8.18.*,>=1.10.2,<1.10.3.0a0,>=1.10.3,<1.10.4.0a0,>=1.8.20,<1.9.0a0']
pytables -> hdf5[version='1.8.18|1.8.18.*,>=1.10.4,<1.10.5.0a0,>=1.8.18,<1.8.19.0a0,>=1.8.18,<1.9.0a0']
hdf5
Package mkl-service conflicts for:
mkl-service
Package importlib_metadata conflicts for:
importlib_metadata
path.py -> importlib_metadata[version='>=0.5']
user31264
  • 6,557
  • 3
  • 26
  • 40
  • Possible duplicate of [How do I install Python OpenCV through Conda?](https://stackoverflow.com/questions/23119413/how-do-i-install-python-opencv-through-conda) – merv Jul 25 '19 at 13:38
  • 4
    Use `conda install -c conda-forge opencv`. Or better, create a new env for it and let Conda resolve the Python version that's best for it: `conda create --name env_name -c conda-forge opencv` – merv Jul 25 '19 at 13:38
  • 1
    @merv - it is not duplicate of that question, because I tried all, I repeat all answers from that question. Neither of them works, which I wrote. – user31264 Jul 25 '19 at 18:52
  • @merv "Use conda install -c conda-forge opencv" - it did not work. In my question there is exactly this command, and what diagnostic it produced. Please read questions more carefully. – user31264 Jul 25 '19 at 18:53
  • Ah sorry about that: your `conda-forge` attempt was below the scroll and Mac Chrome hides scroll bars by default, so it looked like only the `menpo` try was there. I still think creating a new env is a proper solution though. – merv Jul 25 '19 at 20:55
  • Are you still looking for information/answers on this? – AMC Apr 16 '20 at 01:29
  • No, I installed opencv. Don't remember how. It was 8 months ago. – user31264 Apr 16 '20 at 12:24
  • But if you have a valuable information, please answer to help other members. Good answers are appreciated. – user31264 Apr 16 '20 at 12:25
  • @user31264 Do you use the Conda base environment for everything, or were you doing so at the time? – AMC Apr 16 '20 at 19:33
  • yes, i used conda for everything on that computer – user31264 Apr 23 '20 at 23:36
  • 1
    @merv Omg man. I just want to say thank you. I've literally spent the last 12 hours trying to fix a tiny problem and this was the fix. Thank you, you hero – Mogarbobac Jul 07 '21 at 08:47

7 Answers7

18

I have had countless problems with installing opencv with conda This is my approach, create an env if you don't already have one

conda create -n py36 python=3.6
conda activate py36

Install opencv with pip NOT conda

pip install opencv-python

If your still having an issue, uninstall opencv, update ffmpeg

conda install -c conda-forge ffmpeg 

then rerun pip

UPDATE 2020

install pip with you env activate

conda install pip

verify pip is in your env

whereis
pip: /path/anaconda3/envs/your_env/bin/pip

Install opencv with pip

~/anaconda3/envs/your_env/bin/pip3 install opencv-python

Kenan
  • 13,156
  • 8
  • 43
  • 50
  • 2
    _I have had countless problems with installing opencv with conda_ Can you elaborate? Using pip for this should really be a last resort. – AMC Apr 16 '20 at 01:26
  • Does not work at June 2020; results in 'ImportError: DLL load failed: The specified module could not be found'. – Peteris Jun 20 '20 at 22:25
  • 1
    worked ubuntu 2022 thank you – Tom Nov 04 '22 at 16:47
7

Create a complete new environment and let conda deal with compatibilities:

conda create -n cv -c conda-forge opencv matplotlib

This will create a new environment named "cv" with python, opencv and matplotlib.

Today (Out. 11th, 2019) it installed:

  • Python 3.7.3
  • OpenCV 4.1.1
  • MatPlotLib 3.1.1
  • Numpy 1.17.2
  • ... and all other dependencies.
Marcos Lima
  • 96
  • 1
  • 2
4

directly run

pip install opencv-python

on spyder instead

2

I faced similar problem (only hdf5 conflicted). The reason is using incompatible version python.

How about creating new python3.6 environment before installing openCV? You can create new environment like this.

$ conda create -n py36 python=3.6

In addition you should also type this command to activate.

$ activate py36

ashiba
  • 121
  • 6
  • 3
    Why not just create the env from the start around the fact that OpenCV is what you want to install? I.e., `conda create -n opencv_env opencv`. If you want Python 3.6 you can specify that as well: `conda create -n opencv_env opencv python=3.6` – merv Jul 25 '19 at 20:58
  • @merv There is no paticular reason. If I had to say, I didn't know the conda command's notation. – ashiba Jul 26 '19 at 08:09
  • 2
    _How about creating new python3.6 environment before installing openCV?_ You should be using a separate environment for each project anyway. – AMC Apr 16 '20 at 01:28
1

I faced a similar issue. Installed it with the following and it is working for me:

pip install opencv-python

The pypi for the above is at https://pypi.org/project/opencv-python/

0

The easiest way is:

conda install pip
pip install opencv-python

Have fun!

Red One
  • 107
  • 1
  • 5
0

opencv is now available on the main anaconda channel, so contrary to older answers there is now no need to use conda-forge (unless you need other dependencies that are only available from there) or pip (assuming you would prefer to use conda).

To create a new environment named myopencvenv with opencv, use

conda create -n myopencvenv opencv
nekomatic
  • 5,988
  • 1
  • 20
  • 27