Reputation: 129
(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
Upvotes: 12
Views: 54399
Reputation: 39
For me:
PyCharm did a lousy job importing the package. I deleted it and reinstalled it from scratch, which worked.
Upvotes: 0
Reputation: 1
go to site_package directory and remove all cv and opencv packages. then run "pip install opencv-python".
Upvotes: 0
Reputation: 1
This happened to me once when I copied the file via save as. As soon as I deleted the second file, the problem resolved itself.
Upvotes: 0
Reputation: 185
I had this problem, the reason was that my script was named 'cv2.py', let's try to rename to something else.
Upvotes: 6
Reputation: 1
using cv2 on vscode i was having this problem but nothing worked. Turns out that i named my file as "cv2.py" so it was importing itself
"Generally, the Python Circular Import problem occurs when you accidentally name your working file the same as the module name and those modules depend on each other. This way the python opens the same file which causes a circular loop and eventually throws an error."
Upvotes: 0
Reputation: 71
I personally had to uninstall all previous installations dealing with opencv :
pip uninstall opencv-python
pip uninstall opencv-contrib-python
pip uninstall opencv-contrib-python-headless
Then I deleted all files that had to do with opencv too in site-package
after the uninstallation, files related to opencv like opencv_contrib_python-4.6.0.66.dist-info
remained and caused errors.
Once all these files were deleted I reinstalled opencv:
pip install opencv-python
now it works for me
Upvotes: 0
Reputation: 11
The same issue has recently appeared within my code and had no idea of what's happening. Tried different methods such as uninstalling opencv-python
.
Coming to the naming of the file that I am working with, I definitely had no problem that might cause a collapse with the name of the library.
I have deleted the opencv-python
, cleaned all of the package-related caches, and then reinstalled the opencv-python
and opencv-contrib-python
. Now, it is working fine and I can say that no issues have appeared yet.
P.S I am using Arch Linux. If you wonder about how to clear caches, try looking at this source. I hope it helps. Keep learning and exploring, thanks!
Upvotes: 0
Reputation: 1
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
Upvotes: 0
Reputation: 31
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 cv2
statement, not actualcv2
module. This whole thing is causing this whole Error.
Upvotes: 0
Reputation: 1
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
Upvotes: 0
Reputation: 11
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)
Upvotes: 1
Reputation: 165
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
Upvotes: 11
Reputation: 43
Perhaps you have just installed opencv-contrib-python
and not opencv-python
.
Try this out, it worked for me-
pip uninstall opencv-python
pip uninstall opencv-contrib-python
pip install opencv-contrib-python==3.4.2.16
pip install opencv-python==3.4.2.16
You can refer here.
Upvotes: 0
Reputation: 1428
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.
Upvotes: 2
Reputation: 2269
Do cv2.cv2.imread()
This should work in most of the cases. Also, take a look here.
Upvotes: -1
Reputation: 17
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.
Upvotes: 1
Reputation: 196
it works with me:
pip install opencv-python
pip install opencv-contrib-python
source : https://pypi.org/project/opencv-python/
Upvotes: 0
Reputation: 1
Check:
brew list | grep opencv
If it doesn't installed then try:
brew install opencv
Upvotes: 0