'AttributeError: module 'cv2' has no attribute 'VideoCapture'

I had some problems with Opencv - cv2 in Python. This attribute problem also happens with "imread." I tried to uninstall and reinstall with contrib-Opencv,but it did not work. About 2 months ago, my opencv file still worked well, but I don't know why it doesn't work now. In the next reinstallations,this command always sastifies but no good results

import cv2
    cap = cv2.VideoCapture(0)
    cap.set(3,640)
    cap.set(4,480)
    while True:
        success,img = cap.read()
        cv2.imshow("Video",img)
        if cv2.waitkey(1) & 0xFF == ord('q'):
            break

My error command:
Traceback (most recent call last):
  File "C:\Users\Hoang Cao Chuyen\Documents\pyml\cv11.py", line 4, in <module>
    cap = cv2.VideoCapture(0)
AttributeError: module 'cv2' has no attribute 'VideoCapture'
[Finished in 0.2s with exit code 1]
[shell_cmd: python -u "C:\Users\Hoang Cao Chuyen\Documents\pyml\cv11.py"]


Solution 1:[1]

After asking my friend,i had the solution.It is very easy because i saved this file in Documents path in user which my private name Hoang Cao Chuyen without '_'.I changed into another path,and it was OK.

Solution 2:[2]

One of the issue I noticed is that you used cv11 as the main.py name.

It is very easy for PyCharm to get confused if you have a file saved as cv2.py. Please check if you have any other similar files with the name cv2.

Else, try to do this:

  1. remove OpenCV
  2. reinstall using command pip install opencv-contrib-python

OR

try reinstalling ffmpeg as it might be one of the issue

pip install ffmpeg-python

Solution 3:[3]

The problem is that your file is called cv2.py so it imports itself and an error occurs, this can be understood from this line.

Most likely due to a circular import

Which translates as:

Most likely due to circular import

To fix the problem, rename your file

Solution 4:[4]

Add these two lines:

from cv2 import VideoCapture
from cv2 import waitKey

Solution 5:[5]

I had the same problem and was searching for ages. Finally I found a way that is (at least currently) working for me.

When installing opencv there will be a folder 'cv2' which again includes the package 'cv2'.

Installation directory: C:\Users\user\AppData\Local\Programs\Python\Python39\Lib\cv2 Package directory: C:\Users\user\AppData\Local\Programs\Python\Python39\Lib\cv2\cv2

Directory after installation


Probably this leads to confusing for python (maybe some circular import or so). Therefore I just renamed the installation directory to something else, e.g. C:\Users\user\AppData\Local\Programs\Python\Python39\Lib\cv2_

Directory after renaming

That is the solution that is currently (and hopefully in the future as well) working for me).

Solution 6:[6]

The best way to solve all the problems of OPENCV-PYTHON is by uninstalling it and reinstalling it.

Even I faced the same problem.

I fixed it by:

python -m pip uninstall Opencv-python

Then I reinstalled it by using a lower version. But unfortunately, I did not know the versions of opencv; So by using a small trick you can get it by running:

python -m pip install opencv-python==

and you will get an error similar to this:

.45, 3.4.13.47, 3.4.15.55, 3.4.16.57, 3.4.16.59, 3.4.17.61, 3.4.17.63, 4.3.0.38, 4.4.0.40, 4.4.0.42, 4.4.0.44, 4.4.0.46, 4.5.1.48, 4.5.3.56, 4.5.4.58, 4.5.
4.60, 4.5.5.62, 4.5.5.64)
ERROR: No matching distribution found for opencv-python==

Here you can see all the versions of opencv-python; choose any one (but not the latest as the error occurs due the latest version of opencv-python. install it by using:

pip install opencv-python==3.4.17.61 (You can choose your version, but this version solved the issue for me)

then enjoy your coding....

Even AUTO-COMPLETE error in opencv-python gets solved.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Chuyên Hoàng
Solution 2 seraph
Solution 3 Nick
Solution 4 Adrian Mole
Solution 5 Tobi Z
Solution 6 Davide Fiocco