- First install the "Python" plugin
- Install a
pylint
grammar checker
It is recommended to install in the current Python environment of VSCode
$ pip3 install pylint
After the installation is complete, there will be a pylint executable file in the bin directory of the current Python environment
- enable
pylint
grammar checker
Open the configuration file of VSCode
Add the following:
{
// 代码检查
"python.linting.enabled": true,
"python.linting.pylintEnabled": true,
// 保存 Python 文件时检查语法
"python.linting.lintOnSave": true
}
- Configure
pylint
Grammar Checker
Create a .pylintrc file that contains detailed configuration for pylint syntax checking
$ pylint --generate-rcfile > .pylintrc
Where pylint is located in the bin directory of the Python environment (pylint installed in this environment), and the absolute path of pylint may be specified when running the above command
pylint can be used alone
$ pylint --rcfile ~/.pylintrc Test.py
************* Module Base.Test
Test.py:1:0: C0114: Missing module docstring (missing-module-docstring)
Test.py:1:0: C0103: Module name "Test" doesn't conform to snake_case naming style (invalid-name)
--------------------------------------------------------------------
Your code has been rated at 0.00/10 (previous run: 10.00/10, -10.00)
In general, each project will configure a .pylintrc file, move the generated .pylintrc file to the root directory of the project, and add the following content to the VSCode configuration:
{
// 指定 .pylintrc 的路径, ${workspaceFolder} 表示当前项目的根目录
"python.linting.pylintArgs": [
"--rcfile=${workspaceFolder}/.pylintrc"
]
}
–rcfile is an optional configuration content, you can not configure this item, directly use the default configuration item of pylint
If pylint is not installed in the current Python environment of VSCode, you need to add the following additional configuration items (need to be modified to the actual path of pylint)
{
// 指定 pylint 的路径
"python.linting.pylintPath": "${env:HOME}/Miniconda/bin/pylint"
}
- Disable some checks
pylint has over-checking, the following are tips for not writing module documentation strings:
There are 3 levels of ways to disable such prompts:
- Add comments to the code
# pylint: disable=missing-module-docstring
import sys
- Modify the operating parameters of pylint
Modify the configuration of VSCode
The following shows how to disable the invalid-name and missing-module-docstring checks:
{
"python.linting.pylintArgs": [
"--rcfile=${workspaceFolder}/.pylintrc",
"--disable=invalid-name,missing-module-docstring"
]
}
- Modify the .pylintrc file
Search for "disable=" in the .pylintrc file, add a "missing-module-docstring" at the end
disable=raw-checker-failed,
bad-inline-option,
locally-disabled,
file-ignored,
suppressed-message,
useless-suppression,
deprecated-pragma,
use-symbolic-message-instead,
not-callable,
missing-module-docstring