Python 项目工程化最佳实践
VSCode 配置
.editorconfig
配置:
toml
root = true
[*]
indent_size = 2
indent_style = space
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true
[*.py]
indent_size = 4
[*.toml]
indent_size = 4
[*.md]
trim_trailing_whitespace = false
.vscode/extensions.json
配置:
json
{
"recommendations": [
"ms-python.python",
"charliermarsh.ruff",
"fill-labs.dependi"
]
}
.vscode/settings.json
配置:
json
{
"python.analysis.autoImportCompletions": true,
"python.analysis.typeCheckingMode": "basic",
"python.analysis.importFormat": "absolute",
"[python]": {
"editor.defaultFormatter": "charliermarsh.ruff",
"editor.formatOnSave": true,
"editor.codeActionsOnSave": {
"source.fixAll": "explicit",
"source.organizeImports": "explicit"
},
},
"isort.args": [
"--profile",
"black"
],
"files.eol": "\n"
}
工具链
如果没有 uv
请安装 uv
:
bash
pip install uv
使用 uv
创建项目:
bash
uv init
创建虚拟环境:
bash
uv venv
安装依赖:
bash
uv add numpy
安装开发依赖:
bash
uv add --dev pytest
安装项目中的依赖:
bash
uv sync
升级全部依赖:
bash
uv sync -U
导出依赖:
bash
uv export > requirements.txt