0

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.

1
  • you built OpenCV... but not for python3 but for python2. that's the python it got installed in. -- you don't have to build it. just pip3 install opencv-contrib-python to get all the modules. Commented Jun 24, 2022 at 11:17

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

so did you uninstall the version of '4.6.0-dev' ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.