简体   繁体   中英

Windows: OpenCV is installed but getting “Unable to import cv2” error in Python

I realise there are a lot of similar questions on SO, and I have been browsing them but haven't found any success in their solutions so far.

I'm using:

  • Anaconda 1.9.12
  • Python 3.8.3
  • Windows 10

I installed opencv by doing:

pip install opencv-python

When I do

conda list

I can see the following:

opencv                    4.0.1            py38h2a7c758_0
opencv-python             4.3.0.36                 pypi_0    pypi
opencv-python-headless    4.3.0.36                 pypi_0    pypi

And also

py-opencv                 4.0.1            py38he44ac1e_0

So it would seem that opencv is installed. However, when I'm writing python code and I have:

import cv2

I get the "Unable to import cv2" error.

This may have something to do with how Anaconda manages installations and environments, but I had thought everything was installed under my custom environment, since that's where I've been doing everything.

Note: I've also tried other commands such as

conda install -c conda-forge opencv

Which look like they succeed, but then I'm still getting the error.

It looks like you have three different distributions of Open CV installed. This is probably what's causing the conflict here. There is a post on pyimagesearch - pip install opencv which outlines the differences between the different pip packages you can install for Open CV. opencv-python and opencv-python-headless are similar, but opencv-python is used for a system with a GUI, and headless for a system without.

I have replicated these steps on a Windows machine using Anaconda and can confirm Open CV installed and ran successfully.

打开 CV Windows 版本 :

Steps I followed:

  • Open Anaconda Prompt
  • If using a venv, enter conda activate myenv
  • Still in Anaconda prompt, run pip install opencv-python
  • When you use conda list you should see a single open cv present.

In your case, I would try to remove opencv-python-headless and opencv, if you want to use opencv-python. To remove using pip, use the command:

pip uninstall package name

Otherwise it might be worth setting up a new virtual environment in Anaconda and doing a clean install of opencv-python from there.

I used:

  • Anaconda Navigator 1.9.12
  • Python 3.7.7
  • Windows 10

opencv is not compatible with python 3. Try to Install opencv3 in Ananconda.I hope it works.

You can try

conda install -c menpo opencv=3

I ended up doing everything on the base conda environment instead of my custom one (eg conda install opencv), and it started working. I guess something wasn't configured correctly in my custom environment, or it lacked the ability to do something (due to permissions?).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM