The modulenotfounderror: no module named ‘cv2’ happened when the opencv-python isn’t installed before being imported or is installed in the wrong environment. Thus, it throws the Python error “ModuleNotFoundError: No module called ‘cv2′”. Run the
pip install opencv-python command to install the module in order to resolve the issue.
No Module Named ‘cv2’ Occurs For Multiple Reasons
- Not having the opencv-python package installed by running pip install opencv-python.
- Installing the package in a different Python version than the one you’re using.
- Installing the package globally and not in your virtual environment.
- Your IDE running an incorrect version of Python.
- Naming your module cv2.py which would shadow the official module.
- Declaring a variable named cv2 which would shadow the imported variable.
How To Fix No Module Named cv2?
To fix this issue, first open your terminal in your project’s root directory and after that install the opencv-python module.
Here’s the different syntax to fix the issue about No Module Named cv2 in different python versions:
1. In a virtual environment or using python 2.
pip install opencv-python
2. For python 3 (could also be pip3.10 depending on your version).
pip3 install opencv-python
3. If you get permissions error
sudo pip3 install opencv-python pip install opencv-python --user
4. If you don’t have pip in your PATH environment variable.
python -m pip install opencv-python
5. For python 3 (could also be pip3.10 depending on your version).
python3 -m pip install opencv-python
6. Using py alias (Windows).
py -m pip install opencv-python
7. For Anaconda
conda install -c conda-forge opencv
8. For Jupyter Notebook
!pip install opencv-python
After you install the opencv-python module, try importing it like this:
import cv2 print(cv2.__version__)
We have completely discussed about ModuleNotFoundError: No Module Named ‘cv2’ and how to solve this issue. The modulenotfounderror: no module named ‘cv2’ happened when the opencv-python isn’t installed before being imported or is installed in the wrong environment. Thus, it throws the Python error “ModuleNotFoundError: No module called ‘cv2′”.
By the way, if you encounter an error about importing libraries. I have here the list of articles made to solve your problem on how to fix errors in libraries.
- ModuleNotFoundError: No Module Named Pycocotools
- ERROR: Error:0308010c:Digital Envelope Routines::Unsupported
- Only Size-1 Arrays Can Be Converted To Python Scalars
- AttributeError: Module TensorFlow Has No Attribute Contrib
- ‘Smote’ Object Has No Attribute ‘fit_sample
- NameError: Name plot_cases_simple Is Not Defined
- AttributeError: HTMLParser Object Has No Attribute Unescape
By the way, If you have any questions or suggestions about this article, please feel free to comment below.