配置vscode 的Python自动代码检查
-
下载插件
首先在 vscode 中安装以下插件:
https://marketplace.visualstudio.com/items?itemName=ms-python.python
https://marketplace.visualstudio.com/items?itemName=ms-python.black-formatter
https://marketplace.visualstudio.com/items?itemName=ms-python.isort
https://marketplace.visualstudio.com/items?itemName=ms-python.flake8
-
配置 settings
然后快捷键【 Cmd(Ctrl)+,】打开 settings, 右上角直接打开 json 文
在下面融合下面的配置文件
{
"workbench.colorTheme": "Default Light Modern",
"[python]": {
"diffEditor.ignoreTrimWhitespace": false,
"editor.defaultColorDecorators": "auto",
"editor.formatOnType": true,
"editor.wordBasedSuggestions": "currentDocument",
"editor.defaultFormatter": "ms-python.black-formatter",
"editor.formatOnSave": true,
"editor.codeActionsOnSave": {
"source.organizeImports": "explicit"
},
},
"explorer.confirmDelete": false,
"isort.args":["--settings-file", "${workspaceFolder}/pyproject.toml"],
"flake8.args":["--config=${workspaceFolder}/pyproject.toml"],
"black-formatter.args": ["--config", "${workspaceFolder}/pyproject.toml"]
}
成功后的 settings.json 配置文件如下图:

-
安装python 依赖,以下内容形成
pip install isort==6.0.1 black==25.1.0 flake8==7.3.0 pyflakes==3.4.0 flake8-pyproject==1.2.3 pep8-naming==0.15.1 --index-url https://pypi.tuna.tsinghua.edu.cn/simple
或者 把下面内容形成 requirements-dev.txt
isort == 6.0.1
black == 25.1.0
flake8==7.3.0
pyflakes==3.4.0
flake8-pyproject==1.2.3
pep8-naming==0.15.1
--index-url https://pypi.tuna.tsinghua.edu.cn/simple
-
引入插件配置文件
注意,这个配置文件的名字路径,跟第二步的 settings 的配置文件路径是要一致的。
暂时无法在办公平台文档外展示此内容
内容是
[tool.black]
line-length = 120
target-version = ["py310"]
skip-string-normalization = true
[tool.isort]
profile = "black"
line_length = 120
known_third_party = ["sanic", "sanic_ext", "sanic_routing"]
multi_line_output = 3
include_trailing_comma = true
force_grid_wrap = 0
use_parentheses = true
ensure_newline_before_comments = true
[tool.flake8]
max-line-length = 120
exclude = [
".git",
"__pycache__",
"*.pyc",
".mypy_cache",
".pytest_cache",
"build",
"dist",
"venv",
"env"]
ignore = "E203, W503"
extend-select = ["N"]
select = "E, W, F, C, N"
max-complexity = 10
per-file-ignores = ["__init__.py:F401"]

浙公网安备 33010602011771号