How do I get Pylint to recognize NumPy members?

Viewed 101910

I am running Pylint on a Python project. Pylint makes many complaints about being unable to find NumPy members. How can I avoid this while avoiding skipping membership checks?

From the code:

import numpy as np

print np.zeros([1, 4])

Which, when ran, I get the expected:

[[ 0. 0. 0. 0.]]

However, Pylint gives me this error:

E: 3, 6: Module 'numpy' has no 'zeros' member (no-member)

For versions, I am using Pylint 1.0.0 (astroid 1.0.1, common 0.60.0) and trying to work with NumPy 1.8.0.

23 Answers

For ignoring all the errors generated by numpy.core‘s attributes, we can now use:

$ pylint a.py --generated-members=numpy.*

As another solution, add this option to ~/.pylintrc or /etc/pylintrc file:

[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=numpy.*

This feature was introduced in PyLint 1.6.0. It should be noted that code snippet from original question passed linting with this version even without any additional settings. However, this is useful in more complex cases.

If you don't want to add more configuration, please add this code to your configuration file, instead of 'whitelist'.

{
    "python.linting.pylintArgs": ["--generate-members"],
}

I had the same problem with a different module (kivy.properties) which is a wrapped C module like NumPy.

Using Visual Studio Code V1.38.0, the accepted solution stopped all linting for the project. So, while it did indeed remove the false-positive no-name-in-module, it didn't really improve the situation.

The best workaround for me was to use the --ignored-modules argument on the offending module. The trouble is, passing any argument via python.linting.pylintArgs wipes out the default Visual Studio Code settings, so you need to reset those also. That left me with the following settings.json file:

{
    "python.pythonPath": "C:\\Python\\Python37\\python.exe",
    "python.linting.pylintEnabled": true,
    "python.linting.enabled": true,
    "python.linting.pylintArgs": [
        "--ignored-modules=kivy.properties",
        "--disable=all",
        "--enable=F,E,unreachable,duplicate-key,unnecessary-semicolon,global-variable-not-assigned,unused-variable,binary-op-exception,bad-format-string,anomalous-backslash-in-string,bad-open-mode"
    ]
}

This solution worked for me.

Basically, go to select the gear icon from the bottom left → Setting → Workspace Setting → Extension → Python Configuration → click on any Settings.json → add this in the file "python.linting.pylintArgs" : [ "--extension-pkg-whitelist=numpy" ]

I am using Visual Studio Code 1.27.2.

Lately (since something changed in Spyder or Pylint or ?), I have been getting E1101 errors ("no member") from Spyder's static code analysis on astropy.constants symbols. I don't have any idea why.

My simplistic solution for all users on a Linux or Unix system (Mac is probably similar) is to create an /etc/pylintrc file as follows:

[TYPECHECK]
ignored-modules=astropy.constants

Of course, this could, instead, be put in a personal $HOME/.pylintrc file. And, I could have updated an existing file.

It's an old bug with generated-members. If you use another name than numpy, like np or foo, then you must add it in the Pylint configuration (space or comma-separated - no matter), because Pylint doesn't recognize that. So it should look like this:

[pylint]
generated-members=numpy.*,np.*,foo.*

It even throws a full path in no-member error, but the expression needs to be exact. It's an old bug and seems to be fixed soon. Check Issue #2498 in logilab/pylint.

Related