pylint-dev / pylint

It's not just a linter that annoys you!

Home Page:https://pylint.readthedocs.io/en/latest/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

cv2 module members are not recognized

uriva opened this issue ยท comments

commented

When importing cv2 I get errors such as:

[pylint] E1101:Module 'cv2' has no 'imread' member
[pylint] E1101:Module 'cv2' has no 'resize' member
[pylint] E1101:Module 'cv2' has no 'imshow' member
[pylint] E1101:Module 'cv2' has no 'waitKey' member
[pylint] E1101:Module 'cv2' has no 'destroyAllWindows' member

etc'

(the code itself runs fine)

I'm using VSCode 1.26.1 and Python 3.6.5.

See this issue for more complaints: https://github.com/DonJayamanne/pythonVSCode/issues/623

This should work if you pass --extension-pkg-whitelist=cv2 to pylint as in pylint --extension-pkg-whitelist=cv2 .... The reason for this is that cv2 seems to be an extension package, thus it's C code that pylint cannot understand. The extension whitelist flag lets pylint import cv2 in order to build an AST out of the imported cv2 module.

commented

To future readers, in vsc this means adding this line to the settings:
"python.linting.pylintArgs": ["--extension-pkg-whitelist=cv2"]

Thanks @PCManticore

However, even with the extension enables, pylint still doesn't recognise 2nd level members, such as cv2.ml.KNearest_create() or cv2.ximgproc.thinning().

I believe it is the same case as #779 ?

@ashraf-minhaj use the Cmd+shift+p shortcut (or ctrl instead of cmd) and type "open settings (json)". There should be an option which opens the json settings and you can add the line there

I'm encountering the same situation:

dgromov@Predator-G3-571 ~/D/u/s/p/CarND-Advanced-Lane-Lines> pylint camera_calibration.py --extension-pkg-whitelist=cv2 | tail                                                                                                       (SDCND) 
camera_calibration.py:157:18: E1101: Module 'cv2' has no 'undistort' member (no-member)
camera_calibration.py:159:12: E1101: Module 'cv2' has no 'imshow' member (no-member)
camera_calibration.py:160:12: E1101: Module 'cv2' has no 'waitKey' member (no-member)
camera_calibration.py:162:12: E1101: Module 'cv2' has no 'imwrite' member (no-member)
camera_calibration.py:163:12: E1101: Module 'cv2' has no 'destroyAllWindows' member (no-member)
camera_calibration.py:129:0: R1711: Useless return at end of function or method (useless-return)

----------------------------------------------------------------------
Your code has been rated at -12.67/10 (previous run: -12.67/10, +0.00)

dgromov@Predator-G3-571 ~/D/u/s/p/CarND-Advanced-Lane-Lines> conda list | grep  "pylint\|astroid"                                                                                                                                    (SDCND) 
astroid                   2.1.0                    py36_0  
pylint                    2.2.2                    py36_0  

As mentioned, most of the errors should be already solved by using --extension-pkg-whitelist=cv2. But unfortunately, as @cipri-tom mentioned, there are a couple of cv2 modules for which that flag does not work. I think the best way forward is for someone to propose a patch to astroid, the library that pylint uses for infererence, to add support for cv2 in a similar fashion to how we added for numpy in #779. Unfortunately we're not familiar with cv2 so it's not something that we'd do ourselves.

commented

https://www.bilibili.com/video/av33693850/
"python.linting.pylintArgs": ["--generate-members"]

For me, pylint cannot process cv2 when I build cv2 from source. Works when installed using pip.

@mehanton this works for me.

Thank you. :)

  1. On VScode: CTRL + Shift + P
  2. Choose "Preferences: Open Settings (JSON)"
  3. Add this line into JSON file:
    "python.linting.pylintArgs": ["--generate-members"]
  4. Done, it works for me ๐Ÿ‘
  1. On VScode: CTRL + Shift + P
  2. Choose "Preferences: Open Settings (JSON)"
  3. Add this line into JSON file:
    "python.linting.pylintArgs": ["--generate-members"]
  4. Done, it works for me ๐Ÿ‘

Thanks, man! it worked for me too

on VSCode Ubuntu I did "python.linting.pylintArgs": ["--generate-members", "--extension-pkg-whitelist=cv2"] which worked.

  1. On VScode: CTRL + Shift + P
  2. Choose "Preferences: Open Settings (JSON)"
  3. Add this line into JSON file:
    "python.linting.pylintArgs": ["--generate-members"]
  4. Done, it works for me ๐Ÿ‘

Hm.... I can't edit "defaultSetting.json file....

  1. On VScode: CTRL + Shift + P
  2. Choose "Preferences: Open Settings (JSON)"
  3. Add this line into JSON file:
    "python.linting.pylintArgs": ["--generate-members"]
  4. Done, it works for me ๐Ÿ‘

Hm.... I can't edit "defaultSetting.json file....

@insung3511 It must be "settings.json", not "defaultSetting.json"! Make sure you choose "Preferences: Open Settings (JSON)", not "Preferences: Open Default Settings (JSON)" ๐Ÿ‘Œ

thanks

thanks for the help guys

commented

The "--generate-members" option is wrong!
It works because pylint fails.
The command "pylint --generate-members test.py" fails because of "no such option: --generate-members".

commented

Look at this, but the solution mentioned here won't work either.
It seems that the only solution is to disable the "no-member" error.

The "python.linting.pylintArgs": ["--generate-members"] is wrong, it will make pylint not work.
The correct way is to set ""python.linting.pylintArgs": ["--generated-members=cv2.*"] which only config for cv2, reference @bjtho08 reply in #779.

i compiled opencv from soruce and passed the args to pylitn but still intellisense doesnt work, there is another way to solve?

I also facing the same problem of "cv2 module members are not recognized
"
what I did was
after importing cv2
write:
from cv2 import cv2
this worked for me because my JSON is not amendable

  1. On VScode: CTRL + Shift + P
  2. Choose "Preferences: Open Settings (JSON)"
  3. Add this line into JSON file:
    "python.linting.pylintArgs": ["--generate-members"]
  4. Done, it works for me +1

Hm.... I can't edit "defaultSetting.json file....

Now my pylint just don't show any erros ๐Ÿ˜…

The "python.linting.pylintArgs": ["--generate-members"] is wrong, it will make pylint not work.
The correct way is to set ""python.linting.pylintArgs": ["--generated-members=cv2.*"] which only config for cv2, reference @bjtho08 reply in #779.

Thanks that solves it.

  • On VScode: CTRL + Shift + P
  • Choose "Preferences: Open Settings (JSON)"
  • Add this line into JSON file: "python.linting.pylintArgs": ["--generated-members=cv2.*"]
  • Done, it works for me +1

I tried above and still no luck.

https://www.bilibili.com/video/av33693850/
"python.linting.pylintArgs": ["--generate-members"]

works now thanks

The "python.linting.pylintArgs": ["--generate-members"] is wrong, it will make pylint not work.
The correct way is to set ""python.linting.pylintArgs": ["--generated-members=cv2.*"] which only config for cv2, reference @bjtho08 reply in #779.

Thanks that solves it.

  • On VScode: CTRL + Shift + P
  • Choose "Preferences: Open Settings (JSON)"
  • Add this line into JSON file: "python.linting.pylintArgs": ["--generated-members=cv2.*"]
  • Done, it works for me +1

@akash-falcon1999 that pylintarg is wrong, as you can see in the above. It will make any error and any warning from pylint disappear, even those that are supposed to be there. Trust me, you don't want to pass that pylintarg. Better to live with this bug, because it actually is a bug in pylint.

The "python.linting.pylintArgs": ["--generate-members"] is wrong, it will make pylint not work.
The correct way is to set ""python.linting.pylintArgs": ["--generated-members=cv2.*"] which only config for cv2, reference @bjtho08 reply in #779.

Thanks that solves it.

  • On VScode: CTRL + Shift + P
  • Choose "Preferences: Open Settings (JSON)"
  • Add this line into JSON file: "python.linting.pylintArgs": ["--generated-members=cv2.*"]
  • Done, it works for me +1

@akash-falcon1999 that pylintarg is wrong, as you can see in the above. It will make any error and any warning from pylint disappear, even those that are supposed to be there. Trust me, you don't want to pass that pylintarg. Better to live with this bug, because it actually is a bug in pylint.

@bjtho08 Thanks ,rectified it,was quite bugged off with those bugs,although the code was working fine.

  1. On VScode: CTRL + Shift + P
  2. Choose "Preferences: Open Settings (JSON)"
  3. Add this line into JSON file:
    "python.linting.pylintArgs": ["--generate-members"]
  4. Done, it works for me ๐Ÿ‘

Do we need to install json extension in vscode for this?

@rishabh547 see my comment above

@rishabh547 see my comment above

So basically after adding that line in the file save the file right? That's it?

Please help me I am low-key getting depressed because of this error, I have spent hours on this still unable to rectify!

I tried above and still no luck.

Hey did you solve your issue? If so how

@rishabh547 you misunderstood. The line you wrote is wrong and will make pylint fail silently, thus making it appear as if your code has zero errors. You need to use the pylint key-value argument that is written in my comment. That will solve your issue without causing pylint to fail.

This doesn't seem to be bringing up cv2 in the autocomplete suggestions.
Moreoever, hovering over cv2 shows this:
image
I built opencv from source, so this points to the cv2 module under "../opencv/build/python-loader".
While the errors are all gone, no autocomplete suggestions come up. Is this intended?

@SHIVAMMUKHERJEE autocomplete is not handled by pylint, but rather the languageserver. You will have to look at Microsofts languageserver (or jedi, if you use that one) for a solution.

@bjtho08 no dice there, sigh. It's funny that the REPL somehow does this easily.

To future readers, in vsc this means adding this line to the settings:
"python.linting.pylintArgs": ["--extension-pkg-whitelist=cv2"]

how to add this line to settings???

@Shinit5201
image

After reading such complex tasks, I was totally surprised.

A very simple way is:
Instead of import cv2 import from cv2 import cv2

It works like a charm.

After reading such complex tasks, I was totally surprised.

A very simple way is:
Instead of import cv2 import from cv2 import cv2

It works like a charm.

You could also take a step further and import even more granularly into individual classes or features instead of the whole module!

After reading such complex tasks, I was totally surprised.

A very simple way is:
Instead of import cv2 import from cv2 import cv2

It works like a charm.

It worked dude!!๐Ÿ˜

After reading such complex tasks, I was totally surprised.

A very simple way is:
Instead of import cv2 import from cv2 import cv2

It works like a charm.

a simple solution wow thank you

"python.linting.pylintArgs": ["--generate-members"] works for me.

Btw, tts 2020-09-28 already, why people still have to setup this stuff for pylint?

"python.linting.pylintArgs": ["--generate-members"] works for me.

Btw, tts 2020-09-28 already, why people still have to setup this stuff for pylint?

That argument is wrong and will make pylint fail silently. See the comments earlier in the thread for an explanation.

After reading such complex tasks, I was totally surprised.

A very simple way is:
Instead of import cv2 import from cv2 import cv2

It works like a charm.

It won't report an error in VS Code's problems tab any longer that's true. But if you run pylint from your terminal you'll still get

I1101: Module 'cv2.cv2' has no 'imread' member, but source is unavailable. Consider adding this module to extension-pkg-whitelist if you want to perform analysis based on run-time introspection of living objects. (c-extension-no-member)

with from cv2 import cv2 instead of

E1101: Module 'cv2' has no 'imread' member (no-member)

with import cv2. Depending on how one intends to use pylint, whitelisting cv2 in pylint settings as described numerous times above may actually be better.

@Shinit5201
image

this is not working for me I am using Opencv 4.1.0 build from source in an anaconda environment.

  1. On VScode: CTRL + Shift + P
  2. Choose "Preferences: Open Settings (JSON)"
  3. Add this line into JSON file:
    "python.linting.pylintArgs": ["--generate-members"]
  4. Done, it works for me ๐Ÿ‘

Worked for me too. Thanks

I also facing the same problem of "cv2 module members are not recognized
"
what I did was
after importing cv2
write:
from cv2 import cv2
this worked for me because my JSON is not amendable

Thanks! worked for me too

I can't believe how many people here are recommending to edit the clumsy vscode setting "python.linting.pylintArgs".

Not only is it just solving the issue locally, especially when you edit your global vscode settings.json, it also overrides any settings from a .pylintrc file, which is not immediately obvious. Using vscode's settings to add command line arguments to some binary running in the background is a bad idea in general, and should only be done as a last resort.

What you should really do, in my opinion, is to generate a pylint configuration file inside your project folder like this:

pylint --generate-rcfile > .pylintrc

and then edit the settings extension-pkg-allow-list and generated-members there.

You can of course also edit these settings in your user- or system-wide .pylintrc, I'd advise against that though.

I can't believe how many people here are recommending to edit the clumsy vscode setting "python.linting.pylintArgs".

Not only is it just solving the issue locally, especially when you edit your global vscode settings.json, it also overrides any settings from a .pylintrc file, which is not immediately obvious. Using vscode's settings to add command line arguments to some binary running in the background is a bad idea in general, and should only be done as a last resort.

What you should really do, in my opinion, is to generate a pylint configuration file inside your project folder like this:

pylint --generate-rcfile > .pylintrc

and then edit the settings extension-pkg-allow-list and generated-members there.

You can of course also edit these settings in your user- or system-wide .pylintrc, I'd advise against that though.

This. By far the most elegant solution. Thanks!

VS2019 works for me.

install opencv_headless and it works for me.

For pyproject.toml file, add the following

[tool.pylint.typecheck]
generated-members = "cv2.*"

Note that using genrated-members should be the last solution if nothing else work, because it will disable pylint's check on everything coming from cv2.

pip installation is wrong.

Easiest solution and you do not need to worry about any settings or incorrect changes. Much safer.

pip install opencv-python-headless

Try installing opencv-python-headless python dependency instead of opencv-python. That includes a precompiled binary wheel with no external dependencies (other than numpy), and is intended for headless environments like Docker. This saved almost 700mb in my docker image compared with using the python3-opencv Debian package (with all its dependencies).

The package documentation discusses this and the related (more expansive) opencv-contrib-python-headless pypi package.