How to import cv2 in python3?

Python 3.xOpencvNumpy

Python 3.x Problem Overview


I'm using Windows, and I'm trying to install package cv2 for python3.

I did a pip3 install opencv-python and it reports successful:

opencv-python3 is installed

But when I do the import cv2 from python3, it's not found and I get weird errors:

enter image description here

What am I doing wrong?

Python 3.x Solutions


Solution 1 - Python 3.x

Your screenshot shows you doing a pip install from the python terminal which is wrong. Do that outside the python terminal. Also the package I believe you want is:

pip install opencv-python

Since you're running on Windows, I might look at the official install manual: https://breakthrough.github.io/Installing-OpenCV

opencv2 is ONLY compatible with Python3 if you do so by compiling the source code. See the section under opencv supported python versions: https://pypi.org/project/opencv-python

Solution 2 - Python 3.x

Make a virtual enviroment using python3

virtualenv env_name --python="python3"

and run the following command

pip3 install opencv-python

Solution 3 - Python 3.x

There is a problem with pylint, which I do not completely understood yet.

You can just import OpenCV with: from cv2 import cv2

enter image description here

Solution 4 - Python 3.x

Please check your python version. Mine is 3.7.3 on a Mac macOS Catalina Version 10.15.7. If you are using Jupyter notebook, pip3 install opencv-python is enough. In your notebook menu click on Kernel and hit restart. Please see the image I included. Kernel>Restart Then run your code again. In my case that solved the problem.

Solution 5 - Python 3.x

anaconda prompt -->pip install opencv-python

Solution 6 - Python 3.x

well, there was 2 issues: 1.instead of pip, pip3 should be used. 2.its better to use virtual env. because i have had multiple python version installed

Solution 7 - Python 3.x

The best way is to create a virtual env. first and then do pip install , everything will work fine

Solution 8 - Python 3.x

Use '!' before the pip:

! pip install opencv-python

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionclassicdude7View Question on Stackoverflow
Solution 1 - Python 3.xAndrew LongView Answer on Stackoverflow
Solution 2 - Python 3.xHamzaMushtaqView Answer on Stackoverflow
Solution 3 - Python 3.xTamas TuriView Answer on Stackoverflow
Solution 4 - Python 3.xVijai GandikotaView Answer on Stackoverflow
Solution 5 - Python 3.xshinemuView Answer on Stackoverflow
Solution 6 - Python 3.xclassicdude7View Answer on Stackoverflow
Solution 7 - Python 3.xSk07View Answer on Stackoverflow
Solution 8 - Python 3.xSam ModabberView Answer on Stackoverflow