PyLint not recognizing cv2 members
Asked Answered
H

11

88

I am running pylint on an opencv project and I am getting many pylint errors in VS code about members not being present.

Example code:

import cv2
cv2.imshow(....)

Errors obtained:

enter image description here

However , the code runs correctly without any errors.

Versions : pylint 1.8.1 , astroid 1.6.0

Hast answered 30/5, 2018 at 19:30 Comment(0)
A
128

This is from pylint. You can generate a pylint config file in the root of your project with this command: (I find this to be helpful if you work in a team or on different computers from the same repo)

pylint --generate-rcfile > ~/.pylintrc

At the beginning of the generated .pylintrc file you will see

# A comma-separated list of package or module names from where C extensions may
# be loaded. Extensions are loading into the active Python interpreter and may
# run arbitrary code.
extension-pkg-whitelist=

Add cv2 so you end up with

# A comma-separated list of package or module names from where C extensions may
# be loaded. Extensions are loading into the active Python interpreter and may
# run arbitrary code.
extension-pkg-whitelist=cv2

Save the file. The lint errors should disappear.

Archimage answered 19/8, 2018 at 8:56 Comment(2)
If you installed linter extension, may be the file .pylintrc already exists - and it may be empty. You only have to add the line extension-pkg-whitelist=cv2 and save.Gallstone
Does this simply ignore anything to do with cv2 and is it a good thing to do? cv2 is not the only module for whom Pylint will throw this error.Stacked
S
48
  1. On VScode: CTRL + Shift + P
  2. Choose "Preferences: Open Settings (JSON)"
  3. Add this line into JSON file: "python.linting.pylintArgs": ["--generate-members"]

Done, it works for me

Note: Make sure you choose "Preferences: Open Settings (JSON)", not "Preferences: Open Default Settings (JSON)"

Setting File would look like

{
"workbench.iconTheme": "vscode-icons",
"python.dataScience.sendSelectionToInteractiveWindow": true,
"kite.showWelcomeNotificationOnStartup": false,
"python.dataScience.askForKernelRestart": false,
"python.dataScience.jupyterServerURI": "local",
"python.pythonPath": "/usr/bin/python3",
"workbench.colorTheme": "Monokai",
"vsicons.dontShowNewVersionMessage": true,
"python.linting.pylintArgs": ["--generate-members"] }
Statuary answered 10/1, 2020 at 4:59 Comment(10)
There is no option '--generate-members'. I tried this in VS Code, I thought this worked but it turns out pylint actually exits with an error that does not show in VS Code.Calvados
Thanks! It worked like a charm in VSCode 1.55.2.Carroty
Yes, It's working for VS Code version 1.56.2 also on windows 10...Ephrayim
@Clément is correct: no, this is not working. This is breaking pylint, and that is why your lints are disappearing (all of them!). Check yourselves by issueing pylint --generate-members or checking VS Code's Output - Python window. I cannot believe this has been upvoted 40+ times.Full
--generate*d*_members is a valid flag, but takes an argument (cv2.*, in this case).Velamen
@bers: What do you recommend doing other than whitelist cv2, or whatever module whose members Pylint doesn't recognize?Stacked
@Stacked I said nothing against whitelisting cv2. It's just that this answer does not whitelist cv2, but it breaks pylint. It's certainly a way to make lints disappear, but probably not the intended one if it removes all of them.Full
@bers: I was just interested to know if you had another solution other than whitelisting cv2. At the very least, Pylint doesn't recognize the dynamic members of cv2 and numpy.The whitelist may be long.Stacked
@Stacked no, unfortunately.Full
VsCode json style has been changed. As of 2023.09, this should work: "pylint.args": [ "--generate-members" ]Ligate
T
36

Try import cv2 like this:

from cv2 import cv2
Tenpin answered 7/4, 2020 at 10:31 Comment(8)
It would be good if you described what their problem was/ mistake was and how this solves it.Sandman
This does indeed work -- at least for me -- but whyyyy?Revision
why though?! This is hilarious.Tiberias
I won't give you a thumb up until you tell me why it works...Sinking
I love it because this is a simple answer :D however it will be nice if somebody explain why it works :)Gavrielle
Newer pylint versions (2.10.2) simply change the message from no-member to c-extension-no-member and suggests adding the module to "extension-pkg-allow-list" as answered hereNeruda
This....really works.Substation
ImportError: Bindings generation error. Submodule name should always start with a parent module name. Parent name: cv2.cv2. Submodule name: cv2 Praemunire
N
16

Yes it is because the extension has not been installed. Set this: extension-pkg-whitelist=cv2 and you're good to go. However it may not detect the functions or modules implemented in cv2

enter image description here

Nabob answered 14/6, 2018 at 7:12 Comment(2)
it would be nice if you also add code snippets, since images are not searchable, nor copy-paste-ableIndividually
Thanks for the image, way more helpful than a one line code snippet without context.Biocellate
F
14

Here the code snippet for the settings.json file in MS V Code

"python.linting.pylintArgs":["--extension-pkg-whitelist=cv2"]
Felsite answered 10/10, 2019 at 9:33 Comment(1)
This no longer works, pylint has changed the prefix to change commandsFunch
T
8

In VSCode, edit the Settings JSON (Ctrl+Shift+P, > "Preferences: Open Settings JSON)

Then, paste the following into the JSON:

"python.linting.pylintArgs": [
    ... // prievious arguments
    "--generated-members=cv2.*"
]

Don't know why, but other solutions (allowlist, etc) weren't working for me, and I didn't want ot create the .pylintrc file.

Trapp answered 27/7, 2022 at 22:11 Comment(2)
Equivalent to setting generated-members=cv2.* in the .pylintrc.Velamen
Seems for latest versions it should be pylint.args instead of python.linting.pylintArgsMendicant
T
6

As discussed in this closed pylint issue, extension-pkg-whitelist=cv2 doesn't always work. Meanwhile, --generated-members=cv2.* solves the issue most of the time. Therefore, one can configure generated-members in the command line as pylint --generated-members=cv2.* or put the configuration in the pyproject.toml file as

[tool.pylint.typecheck]
# List of members which are set dynamically and missed by pylint inference
# system, and so shouldn't trigger E1101 when accessed. Python regular
# expressions are accepted.
generated-members = ["cv2.*"]

Note, the configuration above can be generated automatically by pylint --generated-members=cv2.* --generate-toml-config according to the official documentation.

Teage answered 23/4, 2023 at 9:11 Comment(0)
B
4

I didn't have to change anything in the pylint Jason file like the most of the answers here My solution is to change the import statement to the form below

from cv2 import cv2

Eventually, cv2 members can be used!

Bronwyn answered 30/12, 2020 at 17:55 Comment(1)
@SimonLundberg I'm actually tried to find an answer .. but I couldn't .. but still works !Bronwyn
F
2

As of 2024, the pylint setting prefixes have changed. The previous answers will not work, citing Unknown Configuration Setting.

Follow the steps from before to open settings.json (Ctrl+Shift+P, > "Preferences: Open Settings JSON) and add this line:

"pylint.args": [
    "--generated-members=(cv2.*)"
],

To update old settings, just change python.linting.pylintArgs to pylint.args.

image showing warning in vscode

Funch answered 13/6 at 20:1 Comment(0)
V
1

I used below config settings in settings.json of vscode and it helped me avoid the unessential flags by pylint, and also got intellisense for cv2 working, it it doesn't work try uninstalling and deleting cv2 packages from C:\Anaconda3\envs\demo1\Lib\site-packages folder, and reinstalling opencv-python package

{
"python.linting.pylintEnabled": true,
  "python.linting.enabled": true,
  "python.linting.pylintArgs": [
    "--extension-pkg-whitelist=cv2"
  ]
}
Velocipede answered 27/11, 2019 at 19:3 Comment(0)
S
0

As of 2024, the following method works for me:

  1. Ctrl-Shift-P to enter the Command Pallette.

  2. Search for "Preferences: Open User Settings (JSON)".

  3. Add the following to the json:

    "pylint.args": [ "--generated-members=(cv2.*)" ]

  4. Save the settings.json.

Note the parenthesis around the "cv2.*" as they are required. Additional modules can be seperated with a comma.

Sard answered 18/6 at 1:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.