How do I get PyLint to recognize numpy members?

2019-01-16 03:56发布

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 .

19条回答
孤傲高冷的网名
2楼-- · 2019-01-16 04:12

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

hg clone https://bitbucket.org/logilab/pylint/
hg clone https://bitbucket.org/logilab/astroid
mkdir logilab && touch logilab/__init__.py
hg clone http://hg.logilab.org/logilab/common logilab/common
cd pylint && python setup.py install

whereby the last step will most likely require a sudo and of course you need mercurial to clone.

查看更多
贼婆χ
3楼-- · 2019-01-16 04:12

A little bit of copy paste from the previous answer to summarize what is working (at least for me: debian-jessie)

  1. In some older version of pylint there was a problem preventing it working with numpy (and other similar packages).

  2. Now that problem has been solved but external C packages (python interfaces to C code -like numpy-) are disabled by default for security reasons.

  3. 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.

查看更多
时光不老,我们不散
4楼-- · 2019-01-16 04:13

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
}
查看更多
SAY GOODBYE
5楼-- · 2019-01-16 04:15

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.*

For mentioned in question code by now this seems reduntant, but still matters for another modules, ie. netifaces and etc.

查看更多
手持菜刀,她持情操
6楼-- · 2019-01-16 04:15

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:

extension-pkg-whitelist=numpy

you can generate a sample .pylintrc by doing:

pylint --generate-rcfile > .pylintrc

and then edit the mentioned line

查看更多
放荡不羁爱自由
7楼-- · 2019-01-16 04:15

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 pylint disable/enable is restricted to a small region of your code
  • That means that you don't have to surround every single line that has an invocation of a numpy function with a pylint directive.
  • You are not doing pylint disable of the error for your whole file, which might mask other issues with your code.

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.

查看更多
登录 后发表回答