" No module named 'cv2' " but it is installed
Question:
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'
Answers:
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)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()
There is 2 possible problems about ModuleNotFoundError: No module named ‘cv2’.
-
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.
-
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.
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%AppDataLocalProgramsPython. 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.
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'
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)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()
There is 2 possible problems about ModuleNotFoundError: No module named ‘cv2’.
-
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.
-
vi /etc/profile
edit the profile and addexport 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.
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%AppDataLocalProgramsPython. 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.