Visual Studio Code
选择使用 black 作为代码格式化工具,保证提交代码风格的统一
1. Install black
pip install black
2. Install black and isort extension for vscode:
3. 设定black及isort的格式化配置
3.1. ctrl + , 打开配置面板
3.2. 在弹出的json配置中添加,"editor.formatOnSave"根据个人需求设置为true或者false
"[python]": {
"editor.defaultFormatter": "ms-python.black-formatter",
"editor.formatOnSave": true,
"editor.codeActionsOnSave": {
"source.organizeImports": true
},
},
"isort.args": [
"--profile",
"black"
],
"python.formatting.blackArgs": [
"--config",
"${workspaceFolder}/pyproject.toml"
]
4. 手动format
1. 可以调整pyproject.toml里的line-length确认是否生效;
2. 手动format命令ctrl+shift+i
创建pyproject.toml如下
[tool.black]
line-length = 88
skip-string-normalization = false
target-version = ['py38']
include = '\.pyi?$'
exclude = '''
(
/(
\.git
| \.vscode
| \.venv
| configs\/
| docker\/
| docs\/
)/
)
'''
[tool.isort]
profile = 'black'