" No module named 'cv2' " but it is installed

Viewed 55634

I have installed the whl file with include opencv + contribution because i want to use the SIFT-algorithm. I installed it with pip in my conda environment, so when i tipp in "conda list" it shows me "opencv-python 3.4.5+contrib " But when I try to run "ModuleNotFoundError: No module named 'cv2'". My Project is also using the correct environment

import cv2
sift = cv2.xfeatures2d.SIFT_create()    
ModuleNotFoundError: No module named 'cv2'
4 Answers

1)Delete all your existing opencv installation

2) Reinstall it again in that way (python 3)

python -m pip install opencv-python

3) And Voila!

> import cv2
> sift = cv2.xfeatures2d.SIFT_create()

Probably you are loading python2 while the conda environment you used contains a python3 executable, or viceversa.

Which executable you are using to execute those lines? If it's something in a system directory, it doesn't have the same libraries you see with a conda list. You can prepend which on linux to get the absolute path of an executable, e.g. which python or which python3.

See also the official conda documentation: https://conda.io/docs/user-guide/getting-started.html#managing-python

Personally I don't like conda, because it tends to mess up too many things, and I usually install everything with pip/virtualenvs or with a pipenv.

There is 2 possible problems about ModuleNotFoundError: No module named 'cv2'.

  1. find the cv2.so file and move it into usr/local/lib/python3.6/site-packages

    find / -name 'cv2.so' possible output /usr/local/lib/python3.6/site-packages/

    cd /usr/local/lib/python3.6/site-packages/

    cp cv2.so /usr/lib/python3.6/site-packages/

    then quit the terminal and rerun it.

  2. vi /etc/profile edit the profile and add export PYTHONPATH=/usr/local/lib/python3.6/site-packages:$PYTHONPATH to the last line.

My device:

  • OS Ubuntu 22.04 LTS

  • package python-is-python3 installed.

The problem and solution which worked for me was related to the location of the packages and Python installation. I had it locally:

/home/me/.local/lib/python3.10/site-packages/cv2

By running with me@mypcname:~/folder-of-cvtest$ python cvtest.py it worked.

But if running sudo python cvtest.py Exactly same error.

So it could be happening the opposite, or Python and Packages be installed on different folders.

Related