7

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'
2
  • how did you install it? how do you run it? Jan 11, 2019 at 14:03
  • For me, it was the way I was running the file on Windows. Running simply example.py may use a different Python version than running via python example.py superuser.com/questions/433897/…
    – Radzor
    Aug 6 at 22:50

6 Answers 6

27

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()
5
  • 8
    For python3 you would want to do; python3 -m pip install opencv-python Jan 11, 2019 at 20:37
  • 3
    Worked for me as of August 23, 2021. To uninstall, I used "pip uninstall opencv-python".
    – lattejiu
    Aug 23, 2021 at 19:27
  • 2
    python3 -m pip install opencv-python Nov 9, 2021 at 7:48
  • 1
    Thanks - after hours of messing around with different approaches, this just worked. (Windows 11, Python 3.10)
    – Tom M
    Apr 13 at 15:47
  • I had the same error. This answer works for me directly on the PC, but does not work in the venv.... Jun 12 at 1:26
3

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.

1
1

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.

1

I met a similar problem today when I switched from opencv-python to opencv-python-headless. Though I can see

opencv-python-headless                  4.7.0.72

from pip list, import cv2 doesn't work.

As stated at the installation and usage of OpenCV at PyPI:

If you installed multiple different packages in the same environment, uninstall them all with pip uninstall and reinstall only one package.

To ensure the step above, the cleanest way I figured out is to remove the python virtual environment completely, with e.g., rm -rf .venv. Then you can re-create a virtualenv using python -m venv .venv and do the installation there.

0

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.

0

If you are certain that you have installed CV2, but you run into "no module named cv2" when running Python, that means CV2 was installed to a different Python version from the one you are running.

You probably have multiple Python versions installed on different folders and your PATH is probably in disarray as well. Different IDEs and editors have different ways to select your Python versions.

If installing OpenCV again doesn't solve it, you can do a clean Python installation. I can't provide a step-by-step guide, but here is the general plan:

  • Uninstall all instances of Python
  • Remove Python from your path. Delete any reference in the PATH to Python
  • Proceed with caution: Delete any remaining files for user-installed Python, not system Python. For Windows, it is C:\Users\%USERNAME%\AppData\Local\Programs\Python. For Mac, the system Python is at /usr/bin/pythonso you should avoid touch this. Extra installations of Python are usually at/usr/local/bin/python` and these are safe to remove.

After this, install any Python version you wish to use. However, I recommend looking into pyenv, virtualenvs and the likes so you can use a specific Python install for a project and minimize the chance of conflicting modules and versions.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.