J.Barker
J.Barker

Reputation: 51

Problems with OpenCV install on Raspberry Pi

I have a Raspberry Pi running Raspian 10. I installed OpenCV using this tutorial. The steps seemed to all complete successfully, but I've had some problems importing the python module. When I use Python 3.7.3, I get the following output:

>>> import cv2
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'cv2'

Which is clearly not working. Using Python 2.7.16 however, it runs fine:

>>> import cv2
>>> cv2.__version__
'4.6.0-dev'

Any idea why this may be? It seems like the OpenCV Python module isn't installed globally for Python 3. Ideally, I'd like to be able to use it in a Python 3 virtual environment.

I do have a temporary workaround. If I cd into the build directory containing the module, I can get it to work:

pi@raspberrypi:~ $ cd opencv/build/python_loader/
pi@raspberrypi:~/opencv/build/python_loader $ ls
cv2  setup.py
pi@raspberrypi:~/opencv/build/python_loader $ python3
Python 3.7.3 (default, Jan 22 2021, 20:04:44) 
[GCC 8.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import cv2
>>> cv2.__version__
'4.6.0-dev'

Obviously, this isn't ideal.

Upvotes: 0

Views: 802

Answers (1)

J.Barker
J.Barker

Reputation: 51

I've managed to answer my own question. Running:

pi@raspberrypi:~/opencv/build $ pip3 install -e python_loader

Installs the package from the folder containing the module. It's not the most elegant solution, and I'd still be interested to see if anyone knows why the original method didn't work.

Hope this can be of some help to someone.

Upvotes: 1

Related Questions