81

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

Kitwradr
  • 1,986
  • 3
  • 18
  • 32

9 Answers9

125

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.

Jules G.M.
  • 3,624
  • 1
  • 21
  • 35
Gabriel P.
  • 3,400
  • 2
  • 32
  • 23
  • 1
    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. – Alejandro Silvestri Jun 23 '20 at 19:40
  • 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. – bytrangle Mar 10 '23 at 10:13
42
  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"] }
Manas shukla
  • 612
  • 5
  • 10
  • 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. – Clément Jan 15 '20 at 06:09
  • Thanks! It worked like a charm in VSCode 1.55.2. – Rasoul May 17 '21 at 07:41
  • Yes, It's working for VS Code version 1.56.2 also on windows 10... – Amar Kumar Jun 04 '21 at 04:28
  • 7
    @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. – bers Jan 19 '22 at 10:51
  • 1
    `--generate*d*_members` is a valid flag, but takes an argument (`cv2.*`, in this case). – kris Aug 19 '22 at 14:33
  • @bers: What do you recommend doing other than whitelist `cv2`, or whatever module whose members Pylint doesn't recognize? – bytrangle Mar 10 '23 at 10:09
  • @bytrangle 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. – bers Mar 10 '23 at 21:20
  • @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. – bytrangle Mar 12 '23 at 04:04
  • @bytrangle no, unfortunately. – bers Mar 12 '23 at 11:10
31

Try import cv2 like this:

from cv2 import cv2
Philippe Remy
  • 2,967
  • 4
  • 25
  • 39
Philip
  • 484
  • 4
  • 9
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

Prajval M
  • 2,298
  • 11
  • 32
  • 4
    it would be nice if you also add code snippets, since images are not searchable, nor [copy-paste-able](https://i.pinimg.com/originals/53/be/34/53be34c987b10821f7c10b9d88436b92.jpg) – Ciprian Tomoiagă Sep 10 '18 at 15:40
  • 3
    Thanks for the image, way more helpful than a one line code snippet without context. – Cross_ Mar 05 '19 at 21:26
14

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

"python.linting.pylintArgs":["--extension-pkg-whitelist=cv2"]
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
Zailux
  • 500
  • 5
  • 12
5

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.

aurelia
  • 493
  • 8
  • 12
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!

Philippe Remy
  • 2,967
  • 4
  • 25
  • 39
Ali.M.Kamel
  • 223
  • 3
  • 14
2

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.

Moore
  • 453
  • 1
  • 6
  • 10
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"
  ]
}