Why is Pylint telling me 'module cv2 has no member'?

13,103

Solution 1

If you don't want to add more config, please add this code to your config file, instead of 'whitelist'.

{
"python.linting.pylintArgs": ["--generate-members"],
}

relevent question: how-do-i-get-pylint-to-recognize-numpy-member

Solution 2

I had the same problem. My setup is Python 3.6.6 64 bit AMD My IDE is Visual Studio Code 1.46.1

My research led me to https://answers.opencv.org/question/200869/e1101module-cv2-has-no-imread-member/. One of the comments on the page linked above says to replace your import statement with one of this format:

from cv2 import cv2

Save your changes and allow your linter to reload. Viola! The error disappears.

Please mark as answer if this helps (I know it's been a while this question was asked).

Cheers.

Share:
13,103
Admin
Author by

Admin

Updated on June 05, 2022

Comments

  • Admin
    Admin about 2 years

    I keep getting pylint errors saying:

    module 'cv2' has no "something" member

    and my cv2 module doesn't work.

    I'm clueless as to why; as far as I know I installed it correctly. I even uninstalled and reinstalled about 20 times.

    When i print out the modules i have in my python lib cv2 is printed; its functions are printed correctly. I'm using python 3.6.6 and don't have another version of python installed.

    For example, this code will print: "the images are the same" no matter what images I'm giving it, as the 2 images will always be equal to "None".

    import cv2
    import numpy as np
    
    image1 = cv2.imread("1.jpg")
    image2 = cv2.imread("2.jpg")
    
    difference = cv2.subtract(image1, image2)
    
    result = not np.any(difference) #if difference is all zeros it will return False
    
    if result is True:
        print ("The images are the same")
    else:
        cv2.imwrite("result.jpg", difference)
        print ("the images are different")
    

    Anyone know whats going on?

  • krflol
    krflol over 5 years
    cv2\cv2 doesn't exist. cv2\data does, hence why the linting should reveal cv2.haarcascades, as it is in the data_init_.py
  • Admin
    Admin over 5 years
    so should i move the init.py? if so where?
  • Admin
    Admin over 5 years
    i have 2 init.py for some reason one in Python\Python36\Lib\site-packages\cv2 and one in Python\Python36\Lib\site-packages\cv2\data
  • krflol
    krflol over 5 years
    Let me clarify, your code works when it runs but the linting doesn't?
  • Admin
    Admin over 5 years
    linting gives an error even tho the module exist as it is in the lib folder, even more advanced tools to print out my python modules did print out the cv2 module and even printing the modules functions was successful and correct, but the module itself doesnt work properly as using the modules functions simply doesnt work as it should
  • krflol
    krflol over 5 years
    "Doesn't work as it should" at runtime? Are you getting an actual exception when you run the script?
  • Admin
    Admin over 5 years
    print(image1)=None same for image2 and diffrence result in undefined error only if i print it tho, so running the code as is will just print "images are the same"
  • krflol
    krflol over 5 years
    Thats a different question, but probably a much better image comparison tutorial can be found here.pyimagesearch.com/2017/06/19/…
  • Admin
    Admin over 5 years
    as i already stated, this code is copy pasted from an image comparison tutorial, its all purpose is to add some information on the problem, plz stick to the problem stated in the question, thank u. btw atm the 2 images are equal to "None" as "imread" isnt recognized
  • Dave W. Smith
    Dave W. Smith over 5 years
    If imread isn't recognized, that code will explode, not return None. If cv.imread() is returning None, then check your filenames/paths. And your questions reads as if you're asking about two problems.
  • Admin
    Admin over 5 years
    What ways are u suggesting for doing so? i tried a few and they all came correct
  • Dave W. Smith
    Dave W. Smith over 5 years
    I suggest checking the im1 and im2 aren't None, and reporting those as "file not found". If both fail to read, cv2.subtract(None, None) will give you None, and np.any(None) likewise returns None. not None is True, causing that code to always report "Images are the same" if both images fail to read.
  • ScipioAfricanus
    ScipioAfricanus almost 5 years
    is there something I can do in pylintrc to add this?
  • serwus
    serwus almost 4 years
    Both import cv2 and from cv2 import cv2 worked, but only the latter prevented my IDE from underlining every cv2 in the code.
  • duff18
    duff18 over 3 years
    as stated by A. J. Paul in an answer below you need to set generated-members=cv2 in your .pylintrc file
  • bers
    bers over 2 years
    This answer is not only wrong, it breaks pylint.