python-project
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChinesePython Project Skill
Python项目开发技能
Modern Python project development using uv package manager and Flask for web components.
使用uv包管理器和Flask构建Web组件的现代Python项目开发方法。
Quick Start
快速开始
New Project with uv
使用uv创建新项目
bash
undefinedbash
undefinedInitialize new project
Initialize new project
uv init my-project
cd my-project
uv init my-project
cd my-project
Add dependencies
Add dependencies
uv add flask pytest ruff mypy
uv add flask pytest ruff mypy
Run application
Run application
uv run python app.py
uv run python app.py
Run with Flask
Run with Flask
uv run flask run --debug
undefineduv run flask run --debug
undefinedPackage Manager: uv
包管理器:uv
Use (astral-sh/uv) for all Python package management. It replaces pip, poetry, pyenv, and virtualenv.
uv使用(astral-sh/uv)进行所有Python包管理工作,它可替代pip、poetry、pyenv和virtualenv。
uvCommon Commands
常用命令
bash
undefinedbash
undefinedProject management
Project management
uv init <name> # Initialize project
uv add <package> # Add dependency
uv remove <package> # Remove dependency
uv sync # Sync dependencies from lockfile
uv lock # Generate lockfile
uv init <name> # Initialize project
uv add <package> # Add dependency
uv remove <package> # Remove dependency
uv sync # Sync dependencies from lockfile
uv lock # Generate lockfile
Running
Running
uv run <command> # Run in project environment
uv run python script.py # Run Python script
uv run pytest # Run tests
uv run <command> # Run in project environment
uv run python script.py # Run Python script
uv run pytest # Run tests
Tools (like pipx)
Tools (like pipx)
uvx <tool> # Run tool in ephemeral env
uv tool install <tool> # Install tool globally
uvx <tool> # Run tool in ephemeral env
uv tool install <tool> # Install tool globally
Python versions
Python versions
uv python install 3.12 # Install Python version
uv python pin 3.12 # Pin version for project
undefineduv python install 3.12 # Install Python version
uv python pin 3.12 # Pin version for project
undefinedWeb Framework: Flask
Web框架:Flask
Use Flask for web applications - lightweight WSGI micro-framework.
使用Flask开发Web应用——轻量级WSGI微框架。
Minimal Flask App
最小化Flask应用
python
undefinedpython
undefinedapp.py
app.py
from flask import Flask, render_template, request, jsonify
app = Flask(name)
@app.route("/")
def index():
return render_template("index.html")
@app.route("/api/data", methods=["GET", "POST"])
def api_data():
if request.method == "POST":
data = request.get_json()
return jsonify({"status": "ok", "received": data})
return jsonify({"message": "Hello, API!"})
if name == "main":
app.run(debug=True)
undefinedfrom flask import Flask, render_template, request, jsonify
app = Flask(name)
@app.route("/")
def index():
return render_template("index.html")
@app.route("/api/data", methods=["GET", "POST"])
def api_data():
if request.method == "POST":
data = request.get_json()
return jsonify({"status": "ok", "received": data})
return jsonify({"message": "Hello, API!"})
if name == "main":
app.run(debug=True)
undefinedFlask Project Structure
Flask项目结构
my_flask_app/
├── app.py # Application entry
├── pyproject.toml # uv project config
├── static/ # CSS, JS, images
│ ├── css/
│ └── js/
├── templates/ # Jinja2 templates
│ ├── base.html
│ └── index.html
└── tests/
└── test_app.pymy_flask_app/
├── app.py # Application entry
├── pyproject.toml # uv project config
├── static/ # CSS, JS, images
│ ├── css/
│ └── js/
├── templates/ # Jinja2 templates
│ ├── base.html
│ └── index.html
└── tests/
└── test_app.pyRun Flask
运行Flask
bash
undefinedbash
undefinedDevelopment
Development
uv run flask run --debug
uv run flask run --debug
With specific host/port
With specific host/port
uv run flask run --host=0.0.0.0 --port=8080
undefineduv run flask run --host=0.0.0.0 --port=8080
undefinedProject Configuration
项目配置
pyproject.toml
pyproject.toml
toml
[project]
name = "my-project"
version = "0.1.0"
description = "Project description"
requires-python = ">=3.11"
dependencies = [
"flask>=3.0",
]
[project.optional-dependencies]
dev = [
"pytest>=8.0",
"ruff>=0.5",
"mypy>=1.10",
]
[tool.ruff]
line-length = 88
target-version = "py311"
[tool.ruff.lint]
select = ["E", "F", "I", "UP"]
[tool.mypy]
python_version = "3.11"
strict = true
[tool.pytest.ini_options]
testpaths = ["tests"]toml
[project]
name = "my-project"
version = "0.1.0"
description = "Project description"
requires-python = ">=3.11"
dependencies = [
"flask>=3.0",
]
[project.optional-dependencies]
dev = [
"pytest>=8.0",
"ruff>=0.5",
"mypy>=1.10",
]
[tool.ruff]
line-length = 88
target-version = "py311"
[tool.ruff.lint]
select = ["E", "F", "I", "UP"]
[tool.mypy]
python_version = "3.11"
strict = true
[tool.pytest.ini_options]
testpaths = ["tests"]Code Quality
代码质量
bash
undefinedbash
undefinedLinting and formatting with ruff
Linting and formatting with ruff
uv run ruff check . # Check for issues
uv run ruff check . --fix # Auto-fix issues
uv run ruff format . # Format code
uv run ruff check . # Check for issues
uv run ruff check . --fix # Auto-fix issues
uv run ruff format . # Format code
Type checking
Type checking
uv run mypy .
uv run mypy .
Testing
Testing
uv run pytest
uv run pytest -v --cov=src
undefineduv run pytest
uv run pytest -v --cov=src
undefinedScripts
脚本
- - Initialize new Python project with standard structure
scripts/init-project.sh - - Set up Flask application boilerplate
scripts/setup-flask.sh
- - 初始化带有标准结构的新Python项目
scripts/init-project.sh - - 搭建Flask应用模板代码
scripts/setup-flask.sh
References
参考资料
- See for complete uv reference
references/uv-commands.md - See for Flask best practices
references/flask-patterns.md
- 完整uv命令参考请查看
references/uv-commands.md - Flask最佳实践请查看
references/flask-patterns.md