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 .
Since this is the top result in google and it gave me the impression that you have to ignore those warnings in all files:
The problem has actually been fixed in the sources of pylint/astroid last month https://bitbucket.org/logilab/astroid/commits/83d78af4866be5818f193360c78185e1008fd29e but are not yet in the Ubuntu packages.
To get the sources, just
whereby the last step will most likely require a
sudo
and of course you need mercurial to clone.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 thatpylint
will never show error related to that packages. In practice,pylint
will not tell you anything about those packages.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.
For ignoring all the errors generated by numpy.core‘s attributes, we can now use:
As another solution, add this option to ~/.pylintrc or /etc/pylintrc file:
For mentioned in question code by now this seems reduntant, but still matters for another modules, ie. netifaces and etc.
In Extension to j_hougs answer, you can now add the modules in question to this line in .pylintrc, which is already prepared empty on generation:
you can generate a sample .pylintrc by doing:
and then edit the mentioned line
This is the pseudo-solution I have come up with for this problem.
Then, in your code, instead of calling
numpy
functions asnp.array
andnp.zeros
and so on, you would writenp_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 followsThen, your application code could import this module only (instead of numpy) as
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 thatnumpy_importer
does not import names that don´t exist innumpy
as those errors won't be caught by pylint.