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 03:59

I was getting the same error for a small numpy project I was working on and decided that ignoring the numpy modules would do just fine. I created a .pylintrc file with:

$ pylint --generate-rcfile > ~/.pylintrc

and following paduwan's and j_houg's advice I modified the following sectors:

[MASTER]

# 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=numpy

and

[TYPECHECK]

# List of module names for which member attributes should not be checked
# (useful for modules/projects where namespaces are manipulated during runtime
# and thus existing member attributes cannot be deduced by static analysis. It
# supports qualified module names, as well as Unix pattern matching.
ignored-modules=numpy

# List of classes names for which member attributes should not be checked
# (useful for classes with attributes dynamically set). This supports can work
# with qualified names.
ignored-classes=numpy

and it "fixed" my issue.

查看更多
我欲成王,谁敢阻挡
3楼-- · 2019-01-16 03:59

I had to add this at the top of any file where I use numpy a lot.

# To ignore numpy errors:
#     pylint: disable=E1101

Just in case someone in eclipse is having trouble with Pydev and pylint...

查看更多
We Are One
4楼-- · 2019-01-16 04:04

In recent versions of pylint you can add --extension-pkg-whitelist=numpy to your pylint command. They had fixed this problem in an earlier version in an unsafe way. Now if you want them to look more carefully at a package outside of the standard library, you must explicitly whitelist it. See here.

查看更多
Fickle 薄情
5楼-- · 2019-01-16 04:06

This has finally been resolved in Pylint 1.8.2. Works out of the box, no pylintrc tweaks needed!

查看更多
爷的心禁止访问
6楼-- · 2019-01-16 04:10

Probably, it's confused with numpy's abstruse method of methods import. Namely, zeros is in fact numpy.core.multiarray.zeros, imported in numpy with statement

from .core import *

in turn imported with

from .numeric import *

and in numeric you'll find

zeros = multiarray.zeros

I guess I would be confused in place of PyLint!

See this bug for PyLint side of view.

查看更多
神经病院院长
7楼-- · 2019-01-16 04:10

A quick answer: update Pylint to 1.7.1 (use conda-forge provided Pylint 1.7.1 if you use conda to manage packages)

I found a similar issue in pylint GitHub here and someone replied everything getting OK after updating to 1.7.1.

查看更多
登录 后发表回答