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'
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.
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
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.
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'
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.
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
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.