5

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?

  • Does `cv2` have the member in question or not? Pylint isn't some infallible god. – jwodder Oct 05 '18 at 15:30
  • "something" is a ref to "imread" "substract" and "imwrite" which exist in the cv2 module –  Oct 05 '18 at 16:16
  • 1
    Have you perhaps named this script, or some other script of yours, `cv2.py`? Pylint might think *that's* what `import cv2` is referring to. Note that `cv2.subtract()` is inappropriate for what you're trying to do anyway, due to saturating math. If the second image is lighter in every single pixel, the difference will be all zeros (rather than negative numbers, which don't exist in the datatype used for pixels). – jasonharper Oct 05 '18 at 16:18
  • its copy and paste from an image comparison guide, anyway its still shouldnt equal "None" right? i checked and i do not have any scripts named cv2 –  Oct 05 '18 at 16:21

6 Answers6

4

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

lai_bluejay
  • 342
  • 3
  • 7
3

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.

  • 1
    Both `import cv2` and `from cv2 import cv2` worked, but only the latter prevented my IDE from underlining every `cv2` in the code. – serwus Jul 02 '20 at 12:11
  • thank you for this response. I had the same issue on Python 3.8 on VS Code but it got solved after changing my import statement from `import load_dictionary` to `from load_dictionary import load`. Also, I had to re-launch VS code editor to fix it all. There is also this [link](https://pylint.readthedocs.io/en/latest/user_guide/messages/error/no-member.html) which I found helpful. – NinaNeu May 11 '23 at 21:02
0

If you navigate through the package, you'll find this directory

yourpythondir\Lib\site-packages\cv2

the cv2 directory doesn't exist, so it has no __init__.py. Linting should work in the python console once the package is imported, but probably not in the text editor

krflol
  • 1,105
  • 7
  • 12
  • cv2\cv2 doesn't exist. cv2\data does, hence why the linting should reveal cv2.haarcascades, as it is in the data\__init__.py – krflol Oct 05 '18 at 17:32
  • so should i move the __init__.py? if so where? –  Oct 05 '18 at 17:36
  • 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 –  Oct 05 '18 at 17:37
  • Let me clarify, your code works when it runs but the linting doesn't? – krflol Oct 05 '18 at 17:38
  • 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 –  Oct 05 '18 at 17:43
  • "Doesn't work as it should" at runtime? Are you getting an actual exception when you run the script? – krflol Oct 05 '18 at 17:47
  • 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" –  Oct 05 '18 at 17:48
  • Thats a different question, but probably a much better image comparison tutorial can be found here.https://www.pyimagesearch.com/2017/06/19/image-difference-with-opencv-and-python/ – krflol Oct 05 '18 at 17:54
0

Ignoring the lint problem, if your goal is to tell whether two images of the same shape are bitwise-identical, phrasing it as

images_differ = np.any(im1 != im2)
if images_differ:
    ...

seems to me to be a clearer statement of intent.

Subtracting one array of uint8 from another is problematic.

Adding: just checked the setup.cfg I'm using, and see that I'm excluding cv2 along with the rest of my dependencies, via

[flake8]
exclude=.git,venv
Dave W. Smith
  • 24,318
  • 4
  • 40
  • 46
  • 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 –  Oct 05 '18 at 18:05
  • 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. – Dave W. Smith Oct 05 '18 at 18:11
  • What ways are u suggesting for doing so? i tried a few and they all came correct –  Oct 05 '18 at 18:14
  • 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. – Dave W. Smith Oct 05 '18 at 18:25
0

You have 2 separate problems:

  • pylint not finding cv2.SOMETHING members
  • images not loaded

It is NOT a problem with your cv2 installation as obviously Python DOES find it (otherwise you'd get a runtime error)

To fix lint problem: It is know that pylint cannot parse cv2 due to reasons: https://github.com/PyCQA/pylint/issues/2426
You need to pass --extension-pkg-whitelist=cv2 to pylint to make the linting errors disappear.

About your image loading problem: Reduce it to a minimal example. E.g. execute python3 -c "import cv2; print(cv2.imread('1.jpg'))" in the console (or put it in a script and execute it) and check the output. If the output is None try an absolute path.

I'm pretty sure your problem comes from the images not being in the path the program has as its working directory. You may validate this by e.g. opening it with standard python: open('1.jpg') and print its (meaningless as binary) content just to see if the file is found.

Flamefire
  • 5,313
  • 3
  • 35
  • 70
0

In your .pylintrc file configuration file replace

generated-members=

with

generated-members=cv2

to suppress these warnings.

Mickael Maison
  • 25,067
  • 7
  • 71
  • 68