AttributeError: partially initialized module ‘cv2’ has no attribute ‘img’ (most likely due to a circular import)

Let’s see how to solve “AttributeError: partially initialized module ‘cv2’ has no attribute ‘img’ (most likely due to a circular import)” error which you may encounter on Python.

There are a few reason on how you may meet such a problem.

First of all it is not only “AttributeError: partially initialized module ‘cv2’ has no attribute ‘img’ (most likely due to a circular import)” issue. I mean it doesn’t have to be ‘img’ which is problematic. You may get “AttributeError: module ‘cv2’ has no attribute ‘imread'” or similar once.

See also  How to solve TypeError: ‘set’ object is not subscriptable

Method 1. Reinstallation of cv2 module

Strat from the easiest way which is to reinstall cv2 module. To do that you need to run

pip uninstall opencv-python
pip uninstall opencv-contrib-python

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

If this will not help you need to remove problematic file.

Method 2. Removal of entire cv2 folder

Go to to your project. For me it is C:\Users\pythoneo\PycharmProjects\Project1\venv\Lib\site-packages\cv2.

See also  Image Blending with OpenCV's addWeighted Function

In this directory find cv2.pyd file and remove it.

Run the same commands again.

pip uninstall opencv-python
pip uninstall opencv-contrib-python

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

This helped me so I believe it will help you as well.