Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to import cv2 in python3?

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?

like image 724
classicdude7 Avatar asked Oct 06 '17 17:10

classicdude7


People also ask

Does Python 3 have cv2?

Python 2.7 is the ONLY supported python version for cv 2.

Where is cv2 in Python?

The file cv2.so is stored in /usr/local/lib/python2. 7/site-packages/... There are also folders in /usr/local/lib called python3. 2 and python2.

Why can't I import cv2 in Python?

importerror no module named cv2 error occurs when cv2 module is not properly installed or its path is not properly set or configured. The straight way fix for this error (no module named cv2) is to reinstall this module (OpenCV-python). In some scenario reinstalling this module automatically remove the older version.


2 Answers

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

like image 141
Andrew Long Avatar answered Sep 18 '22 11:09

Andrew Long


Make a virtual enviroment using python3

virtualenv env_name --python="python3" 

and run the following command

pip3 install opencv-python 
like image 32
HamzaMushtaq Avatar answered Sep 18 '22 11:09

HamzaMushtaq