No module named cv2 error in Python Raspberry

Question:

I am having this error when I try to run a Python file. I tried opening a new terminal and importing cv2 worked there in Python but doesn’t work in my OpenCV virtual environment. I don’t even understand how virtualenvs work but I have to find a solution for this.

> (cv) pi@raspberrypi:~/Camerafeed $ python run.py Traceback (most
> recent call last):   File "run.py", line 2, in <module>
>     from camerafeed import CameraFeed   File "/home/pi/Camerafeed/camerafeed/__init__.py", line 5, in <module>
>     import cv2 ImportError: No module named 'cv2'
Asked By: Erdem Efe Erol

||

Answers:

python -c "import cv2" is a quick indicator to import module, you can use it in either of your environments as a check.

My guess is that you should install the module while you are in (cv) environment by running pip install opencv-python at command prompt.

Answered By: Evgeny

I believe numpy is already installed. If not then sudo apt-get install python-numpy to install opencv2

Now you can run this command to install Opencv:

sudo apt update && sudo apt-get install python-opencv python-scipy ipython

For more help you can also refer to this link

Answered By: cyber-security47

I referred below link to solve the error of "no module Named cv2":

https://qengineering.eu/install-opencv-4.4-on-raspberry-pi-4.html

I copied manually the cv2 as suggested by the above link as below:

For Python 2:
$ cd ~/opencv/build/lib/ $ sudo cp cv2.so /usr/local/lib/python2.7/dist-packages/cv2/python-2.7
For Python 3:
$ cd ~/opencv/build/lib/python3 $ sudo cp cv2.cpython-37m-arm-linux-gnueabihf.so /usr/local/lib/python3.7/dist-packages/cv2/python-3.7
Ensure that you create cv2 dir first if you got error.

Answered By: user14848773
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.