'AttributeError: module 'cv2' has no attribute 'imread'
(i'm on mac os 10.8.5)
I'm using Python 3 (through jupyter notebook) and trying to import cv2
I did import cv2 succefully, but when I type im_g = cv2.imread("smallgray.png", 0) I get this error:
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-7-5eb2880672d2> in <module>()
----> 1 im_g = cv2.imread("smallgray.png", 0)
AttributeError: module 'cv2' has no attribute 'imread'
I also checked dir(cv2) and I get:
['__doc__', '__loader__', '__name__', '__package__', '__path__', '__spec__'
I guess a lot of functions are missing...
Is it due to a wrong opencv installation ?
Actually I struggled a lot to get opencv and I guess I installed it 'too many times' and different ways via Terminal. (brew, pip)
Should I uninstall opencv and start over ? How can I do this properly ?
Thx in advance
Solution 1:[1]
It might be the case that you installed it in the wrong way. In my case as well, I installed OpenCV wrongly so I uninstalled it completely then reinstalled it which caused it to work.
Please notice the sequence of uninstalling and installing:
Uninstall:
pip uninstall opencv-python
pip uninstall opencv-contrib-python
Install:
pip install opencv-contrib-python
pip install opencv-python
Solution 2:[2]
I was have this problem too, and i solved it, the problem was been in i named my script cv2.py, i'm just rename it to something else.
Solution 3:[3]
Reader's problem could be, that a wrong library (cv2 package) has been installed.
I installed opencv-python3 instead of opencv-python for example.
So in case you have PyCharm IDE, go to File->Settings->Project: *->Python Interpreter and see what you have listed there:
Make sure, you have installed opencv-python. If you have also other similar names, it should be fine - in my case it works.
Solution 4:[4]
This might happen if you have named one of your files as 'cv2.py'. So when you 'import cv2', the system accesses your file instead of the actual library. So try renaming cv2.py' to any other name and your code should work fine.
Solution 5:[5]
Do cv2.cv2.imread()
This should work in most of the cases. Also, take a look here.
Solution 6:[6]
Check:
brew list | grep opencv
If it doesn't installed then try:
brew install opencv
Solution 7:[7]
it works with me:
pip install opencv-python
pip install opencv-contrib-python
Solution 8:[8]
I had the same issue, this turned out to solve the problem:
from cv2 import cv2
im_g=cv2.imread("smallgray.png", 0)
print(im_g)
Solution 9:[9]
I faced Similar issue,On Ubuntu
I had installed open-cv using the comand:
sudo pip3 install opencv-python3
I Uninstalled opencv, By:
sudo pip3 uninstall opencv-python3
Then reinstalled it Using the following Code:
pip3 install opencv-python
Solution 10:[10]
I also faced this problem.
And get a solution by changing the current cv2.py to cv.py(You can name whatever you like except the name of packages in use).
Little Explanations
Saving the current working file as the name of cv2.py, when we try to run the file it imports the current file with
import cv2statement, not actualcv2module. This whole thing is causing this whole Error.
Solution 11:[11]
The main problem is that you have your file or folder named "cv2", it is not permitted in Python. The way to solve is change the name and then your code will be run
Solution 12:[12]
Perhaps you have just installed opencv-contrib-python and not opencv-python.
Try this out, it worked for me-
- First uninstall:
pip uninstall opencv-python
pip uninstall opencv-contrib-python
- then install:
pip install opencv-contrib-python==3.4.2.16
pip install opencv-python==3.4.2.16
You can refer here.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow

