python encountered AttributeError: module 'XXX' has no attribute 'XXX' error

wrong reason:

There may be two main reasons:

1. Check if there is any misspelled function, sometimes it is really possible to make a mistake if you write it too fast;

2. If the spelling is correct, check whether the function of the module has been updated, because the third-party library of python is updated from time to time, and sometimes the function name will change slightly, which is normal. The solution is to check Official documents under the corresponding version to see if the function has changed

3. If it is still correct after the first two steps, then it is very likely that the name of your python file is the same as the third-party library you use. This is a mistake that Xiaobai often encounters. This is the reason for writing this article.

For example: if you use the cv2 library, that is, there is an import cv2 statement, and the file name of your program is also cv2.py, then when you use cv2.imshow(), you actually call the program you wrote instead of The real cv2 library, so of course it will report the error of has no attribute 'XXX'

The solution is to change the file name.

Guess you like

Origin blog.csdn.net/weixin_44517500/article/details/105853531