10

(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

Samuel
  • 109
  • 1
  • 2
  • 9

16 Answers16

10

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
Elletlar
  • 3,136
  • 7
  • 32
  • 38
Satyam Annu
  • 155
  • 1
  • 5
  • 1
    concurrent installation of opencv packages can cause conflicts. never install more than one of the variants. they all contain the base modules. – Christoph Rackwitz May 17 '23 at 10:36
5

I had this problem, the reason was that my script was named 'cv2.py', let's try to rename to something else.

KR1470R
  • 111
  • 3
  • 8
2

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:

enter image description here

Make sure, you have installed opencv-python. If you have also other similar names, it should be fine - in my case it works.

Sold Out
  • 1,321
  • 14
  • 34
  • In case you uninstall some other package (like opencv-python3), likely this step will corrupt your dependencies and you may get that error again. In such a case just uninstall and install the opencv-python again.. – Sold Out Oct 16 '20 at 15:40
1

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.

1

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)
0

Check:

brew list | grep opencv

If it doesn't installed then try:

brew install opencv
0

it works with me:

pip install opencv-python

pip install opencv-contrib-python

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

Jawad
  • 186
  • 1
  • 14
0

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
0

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.

Pijush Barman
  • 31
  • 1
  • 5
0

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

0

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!

Jasurbek16
  • 11
  • 5
0

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

ken
  • 31
  • 4
0

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

  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Nov 29 '22 at 13:01
0

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.

Neena
  • 1
  • 1
-1

Do cv2.cv2.imread()

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

lucians
  • 2,239
  • 5
  • 36
  • 64
  • 1
    I tried and this is what I get: `--------------------------------------------------------------------------- AttributeError Traceback (most recent call last) in () ----> 1 im_g = cv2.imread("smallgray.png", 0) AttributeError: module 'cv2' has no attribute 'imread'` I also tried to rename `cv2.so` (in /site packages/cv2/) to `cv2.py` and got the same error. – Samuel Dec 17 '17 at 20:47
  • @lotusam try do the installation again – lucians Dec 17 '17 at 21:57
-1

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.

Grayrigel
  • 3,474
  • 5
  • 14
  • 32
Subhodeep
  • 33
  • 1
  • 9