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 check
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. No idea why.
My simplistic solution for all users on a Linux or Unix system (Mac is probably similar) is to create an /etc/pylintrc 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.
A little bit of copy paste from the previous answer to summarize what is working (at least for me: debian-jessie)
In some older version of pylint
there was a problem preventing it working with numpy (and other similar packages).
Now that problem has been solved but external C packages (python interfaces to C code -like numpy-) are disabled by default for security reasons.
You can create a white list, to allow pylint
to use them in the file ~/.pylintrc
.
Basic command to run: # ONLY if you do not already have a .pylintrc file in your home $ pylint --generate-rcfile > .pylintrc
Then open the file and add the packages you want after extension-pkg-whitelist=
separated by comma. You can have the same behavior using the option --extension-pkg-whitelist=numpy
from the command line.
If you ignore some packages in the [TYPECHECK]
section that means that pylint
will never show error related to that packages. In practice, pylint
will not tell you anything about those packages.
This is the pseudo-solution I have come up with for this problem.
#pylint: disable=no-name-in-module
from numpy import array as np_array, transpose as np_transpose, \
linspace as np_linspace, zeros as np_zeros
from numpy.random import uniform as random_uniform
#pylint: enable=no-name-in-module
Then, in your code, instead of calling numpy
functions as np.array
and np.zeros
and so on,
you would write np_array
, np_zeros
, etc.
Advantages of this approach vs. other approaches suggested in other answers:
The clear disadvantage is that you have to explicitely import every numpy function you use.
The approach could be elaborated on further.
You could define your own module, call it say, numpy_importer
as follows
""" module: numpy_importer.py
explicitely import numpy functions while avoiding pylint errors
"""
#pylint: disable=unused-import
#pylint: disable=no-name-in-module
from numpy import array, transpose, zeros #add all things you need
from numpy.random import uniform as random_uniform
#pylint: enable=no-name-in-module
Then, your application code could import this module only (instead of numpy) as
import numpy_importer as np
and use the names as usual: np.zeros
, np.array
etc.
The advantage of this is that you will have a single module in which all numpy
related imports are done once and for all, and then you import it with that single line, wherever you want. Still you have to be careful that numpy_importer
does not import names that don´t exist in numpy
as those errors won't be caught by pylint.
I'm not sure if this is a solution, but in VSCode once I wrote explicitly in my user settings to enable pylint, all modules were recognized.
{
"python.linting.pep8Enabled": true,
"python.linting.pylintEnabled": true
}
If using Visual Studio Code with Don Jayamanne's excellent Python extension, add a user setting to whitelist numpy:
{
// whitelist numpy to remove lint errors
"python.linting.pylintArgs": [
"--extension-pkg-whitelist=numpy"
]
}
There have been many different bugs reported about this over the past few years i.e. https://bitbucket.org/logilab/pylint/issue/58/false-positive-no-member-on-numpy-imports
I'd suggest disabling for the lines where the complaints occur.
# pylint: disable=E1103
print np.zeros([1, 4])
# pylint: enable=E1103