Skip to content

cv2 module members are not recognized #2426

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
uriva opened this issue Aug 20, 2018 · 62 comments
Closed

cv2 module members are not recognized #2426

uriva opened this issue Aug 20, 2018 · 62 comments

Comments

@uriva
Copy link

uriva commented Aug 20, 2018

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

@PCManticore
Copy link
Contributor

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.

@uriva
Copy link
Author

uriva commented Aug 21, 2018

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

@cipri-tom
Copy link

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 ?

@cipri-tom
Copy link

@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

@Davidnet
Copy link

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  

@PCManticore
Copy link
Contributor

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.

@mehdimammadov
Copy link

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

@satyajithj
Copy link

satyajithj commented May 3, 2019

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

@yeknafar
Copy link

yeknafar commented Jun 9, 2019

@mehanton this works for me.

Thank you. :)

@LintangWisesa
Copy link

  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 👍

@githarshp
Copy link

  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

@nyck33
Copy link

nyck33 commented Jul 23, 2019

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

@insung3511
Copy link

  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....

@LintangWisesa
Copy link

  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)" 👌

@duttanaman1
Copy link

thanks

@funny860
Copy link

thanks for the help guys

@Blinue
Copy link

Blinue commented Aug 21, 2019

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".

@Blinue
Copy link

Blinue commented Aug 21, 2019

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.

@rivergold
Copy link

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.

@t31060
Copy link

t31060 commented Mar 6, 2020

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

@Saksham2dubey
Copy link

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

@JulianToledano
Copy link

  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 😅

@JulianToledano
Copy link

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

@Vivraan
Copy link

Vivraan commented Aug 7, 2020

@Shinit5201
image

@blackhawk005
Copy link

blackhawk005 commented Aug 7, 2020 via email

@kshitij-agarwal
Copy link

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.

@Vivraan
Copy link

Vivraan commented Sep 4, 2020

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!

@kritikaparmar-programmer

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!!😁

meashishsaini added a commit to meashishsaini/up_scholarship that referenced this issue Sep 19, 2020
For cv2 reference: pylint-dev/pylint#2426

Signed-off-by: Ashish Saini <sainiashish08@gmail.com>
@slugb0t
Copy link

slugb0t commented Sep 26, 2020

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

@zchrissirhcz
Copy link

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

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

@bthorsted
Copy link

"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.

@jofrev
Copy link

jofrev commented Oct 11, 2020

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.

@gautamjain1009
Copy link

@Shinit5201
image

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

@Mechazo11
Copy link

  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

@ardf
Copy link

ardf commented Apr 29, 2021

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

@nnmrts
Copy link

nnmrts commented Sep 5, 2021

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.

@domajstorovic
Copy link

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!

@KukavicaHome
Copy link

VS2019 works for me.

cbm755 added a commit to plomgrading/plom that referenced this issue Jul 5, 2022
I read [1], [2] and then tried this...

[1] pylint-dev/pylint#3437
[2] pylint-dev/pylint#2426

Interesting [3] seems to recommend what we used to do `from cv2 import
cv2` or somesuch...

[3] pylint-dev/pylint#2426 (comment)
@qngv
Copy link

qngv commented Jul 14, 2022

install opencv_headless and it works for me.

@ZhiliangWu
Copy link

For pyproject.toml file, add the following

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

@Pierre-Sassoulas
Copy link
Member

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.

@knowledgeLover
Copy link

knowledgeLover commented Jul 16, 2023

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.

@DungGramer
Copy link

Update 2023:

  "pylint.args": ["--extension-pkg-whitelist=cv2"]

@LambdaScorpii
Copy link

Hello There,

since the new release of OpenCV this no longer works. It still throws the no member error, regardless if you put in "--extension-pkg-whitelist=cv2" or --extension-pkg-allow-list=cv2 (as suggested in the official documentation, as "whitelist"seems dprecated). Any suggestion how to make that work?

Generating all members defies the purpose of PyLint in my opinion...

@Pierre-Sassoulas
Copy link
Member

Pierre-Sassoulas commented Jul 1, 2024

Could you open a new issue with a reproducer please @LambdaScorpii ? (Edit: Sorry Bad ping my Bad)

@SajjadAemmi
Copy link

SajjadAemmi commented Nov 4, 2024

Option 1

Add --generated-members argument when you run pylint command:

pylint --generated-members=cv2.* client.py

Option 2

Run:

pylint --generate-rcfile > .pylintrc

Then open .pylintrc, find generated-members and write:

generated-members="cv2.*"

@LambdaScorpii
Copy link

hi @SajjadAemmi,

Thanks for the advice. Unfortubnately, this won't work well because, a) you annot configure this in a persistent manner in the pylint extenstion and b) this removes every scope on linting when it starts with cv2 regardless if right or wrong, so kind of defies the point of the whole linting.

@ewwink
Copy link

ewwink commented Jan 22, 2025

this work for me in 2025, in settings search pylint.args then add --generated-members=cv2.*

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests