简体   繁体   中英

How do I configure Pylint with VSCode?

For my setup, I have Pylint ==3.0.0a4 and VSCode Version: 1.67.2. In my VSCode settings.json, I have

"editor.formatOnSave": true,
"python.linting.enabled": true,
"python.linting.pylintEnabled": true,

and every time I save a.py file, VSCode runs the following command:

~/.local/share/virtualenvs/hello-world-n4ICiflw/bin/python ~/.vscode/extensions/ms-python.python-2022.6.2/pythonFiles/linter.py -m pylint ~/code/hello-world/src/main.py

which I believe is VSCode running the Python linter via a proxy call to linter.py -m pylint using --output-format=json by default, but it fails with the following error

##########Linting Output - pylint##########


[ERROR 2022-4-30 15:11:59.266]: Linter 'pylint' failed to parse the output '. [SyntaxError: Unexpected end of JSON input
    at JSON.parse (<anonymous>)
    at s.parseMessages (/Users/kevinyang/.vscode/extensions/ms-python.python-2022.6.2/out/client/extension.js:2:518074)
    at s.run (/Users/kevinyang/.vscode/extensions/ms-python.python-2022.6.2/out/client/extension.js:2:499508)
    at async s.runLinter (/Users/kevinyang/.vscode/extensions/ms-python.python-2022.6.2/out/client/extension.js:2:517589)]

It appears as though the linter.py cannot handle the JSON output produced by pylint.

I also tried installing the Pylint Extension, but ran into the same problem.

Running $ pylint src/main.py manually on a shell works just fine.

Has anybody else come across this problem and solved it?

Solved this by downgrading pylint to ==2.13.4. Seems like it's a known issue in recent versions of Pylint.

The problem in my case:

Incorrect pylintrc file path configuration.

Troubleshooting :

  1. Look at Output->Python logs

  2. Try running the pylint command that is being used there:

    pylint --rcfile=~/projects/../my_project/pylintrc ~/projects/../my_project/a_script_to_analyze.py

  3. See the error (which is not a Json object, that's why you are getting this weird Json error):

" The config file /home/../my_projects/.pylintrc doesn't exist! "

Solution in my case: Modify your workspace settings to use a valid pylintrc file

"python.linting.pylintArgs": [
            "--rcfile=${workspaceFolder}/pylintrc"
        ]

Check the vscode setting for python.linting.pylintPath . I was using Git Bash for Windows, and had python.linting.pylintPath incorrectly set to /c/Python39/Scripts/pylint . It didn't like the forward slashes.

I had to change it to c:\Python39\Scripts\pylint . Or better yet, just leave the setting blank. vscode should be able to find the correct version itself.

Look at the vscode Output > Python window to see the error messages.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM