How do I get PyLint to recognize numpy members?

前端 未结 22 879
南旧
南旧 2020-11-28 20:29

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

相关标签:
22条回答
  • 2020-11-28 20:54

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

    {
    "python.linting.pylintArgs": ["--generate-members"],
    }
    
    0 讨论(0)
  • 2020-11-28 20:54

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

    Using VSCode 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. Trouble is, passing any argument via python.linting.pylintArgs wipes out the default VSCode settings, so you need to re-set 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"
        ]
    }
    
    0 讨论(0)
  • 2020-11-28 20:56

    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.

    0 讨论(0)
  • 2020-11-28 20:57

    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.

    0 讨论(0)
  • 2020-11-28 20:58

    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.

    0 讨论(0)
  • 2020-11-28 20:58

    This solution worked for me

    Basically, go to Select the gear icon from 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 VS 1.27.2

    0 讨论(0)
提交回复
热议问题