简体   繁体   中英

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

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

我也有这个问题,我解决了,问题出在我把我的脚本命名为 cv2.py,我只是把它重命名为别的东西。

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.

Do cv2.cv2.imread()

This should work in most of the cases. Also, take a look here .

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.

Check:

brew list | grep opencv

If it doesn't installed then try:

brew install opencv

it works with me:

pip install opencv-python

pip install opencv-contrib-python

source : https://pypi.org/project/opencv-python/

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)

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

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 actual cv2 module. This whole thing is causing this whole Error.

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

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.

PS 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!

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 and opencv-contrib:

pip install opencv-contrib-python
pip install opencv-python

now it works for me

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."

Perhaps you have just installed opencv-contrib-python and not opencv-python .

Try this out, it worked for me-

  1. First uninstall:
pip uninstall opencv-python
pip uninstall opencv-contrib-python
  1. then install:
pip install opencv-contrib-python==3.4.2.16
pip install opencv-python==3.4.2.16

You can refer here .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM