Python: import cv2 .. No module named 'cv2'

pip install opencv-python

it says:

Successfully installed numpy and opencv-python

Good.

I then run .py:

import cv2

It says:

No module named ‘cv2’

What am I doing wrong?

:slight_smile:

2 Likes

What OS are you on? :slight_smile:
If your on raspberryPiOS it may be you need to set up an environment where the modules can be sand-boxed for your code.

1 Like

Hey there, @Bill293536.

As Pixmusix said, it could be down to a Virtual Environment issue, but I’m curious what tutorial are you using?

Every tutorial I have seen on using opencv asks uses to manually download the opencv from github. I’ve never seen any guide recommend installing it via pip.

Pixmusic, Jane,

Thank you.

I will do core opencv again and see if that helps.

:slight_smile:

Bill

Pixmusic, Jane,

It works!

I now have a problem.

:frowning:

from picamera2 import Picamera2

No module named ‘libcamera._libcamera’

What do I do?

:slight_smile:

Just so you have: How to set up YOLO Computer Vision on a Raspberry Pi | Conda & Ultralytics

Easy fix, you just need to install it.

From context I think you are using Raspberry Pi Os.
I’ve learned that, as a rule of thumb, if the package has “lib” in the name it probably wants to be installed system wide. On Debian based systems, like RP Os, apt is right tool for this.

sudo apt install python3-libcamera
1 Like

Pixmusix,

I put the:

sudo apt install python3-libcamera

into the Terminal, and the last item is:

python3-libcamera set to manually installed

Is still fails of:

from picamzero import Camera

:frowning:

What should I do?

:slight_smile:

1 Like

Hey @Bill293536,

I noticed that your error message occured on a different import than previously. In your last post, you said:

But in this reply, you said the error is on:

  1. Have you changed anything else in the intervening time between your last post and now.
  2. Could you post a screenshot of the exact error message that you are receiving?
  3. Can I just confirm as well that when you did this command as in the tutorial:
echo '/usr/lib/python3/dist-packages' > /home/pi/miniforge3/envs/ultralytics-env/lib/python3.11/site-packages/system-packages.pth

That you replaced the ‘pi’ directory with the home directory that you’re using on the Pi.

Jane,

Thank you.

:slight_smile:

Right,

ra5@ra5:~ $ sudo apt install python3-libcamera

The result was:

…python3-libcamera set to manually installed

What should I do?

My .py (Python) was:

from picamzero import Camera

Shell:

from ._libcamera import *

ModuleNotFoundError: No module named ‘libcamera._libcamera’

What should I do?

:slight_smile:

Bill

1 Like

pi → ra5

1 Like

Hey there, Bill,

Are you sure that it’s meant to be:

from picamzero import Camera

If you’re using this guide then the module is listed as picamerazero2 and the import command is:

from picamera2 import Picamera2
1 Like

Jane,

Thank you.

Uniformity, both give the same result!

ModuleNotFoundError: No module named ‘libcamera._libcamera’

Both:

from picamzero import Camera

and

from picamera2 import Picamera2

What do you think I should do?

:slight_smile:

Bill

Hi Bill,

Lets take a step back and make sure all the pieces are in the right place.

The following command will check to see if the _libcamera module is on your system:

dpkg -L python3-libcamera | grep _libcamera

If this returns nothing then -libcamera is not properly installed on your system, in which case you should try installing python3-libcamera again.

Another common cause of this is having your code and modules be on different sides of a virtual environment. The following terminal commands will run a quick test to see if this works from a command line:

python3 -c "import libcamera; print('libcamera ok')"

python3 -c "from picamzero import Camera; print('picamzero ok')"

if everything is set up correctly, these commands should both work without errors from inside of your virtual environment.

Let us know how this goes!

Hi Samuel,

I had tried the commands you listed.

:slight_smile:

The first one listed the .libcam

:slight_smile:

The second and the third listed:

No module named: libcam

:frowning:

(ultralytics-env) ra5@ra5

What should I do?

Bill

python3 -c "import libcamera; print('libcamera ok')"

python3 -c "from picamzero import Camera; print('picamzero ok')"

Both give:

ModuleNotFoundError: No module named ‘libcamera._libcamera’

What should I do?

:slight_smile: