python-project

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Python 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
undefined
bash
undefined

Initialize 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
undefined
uv run flask run --debug
undefined

Package Manager: uv

包管理器:uv

Use
uv
(astral-sh/uv) for all Python package management. It replaces pip, poetry, pyenv, and virtualenv.
使用
uv
(astral-sh/uv)进行所有Python包管理工作,它可替代pip、poetry、pyenv和virtualenv。

Common Commands

常用命令

bash
undefined
bash
undefined

Project 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
undefined
uv python install 3.12 # Install Python version uv python pin 3.12 # Pin version for project
undefined

Web Framework: Flask

Web框架:Flask

Use Flask for web applications - lightweight WSGI micro-framework.
使用Flask开发Web应用——轻量级WSGI微框架。

Minimal Flask App

最小化Flask应用

python
undefined
python
undefined

app.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)
undefined
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)
undefined

Flask 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.py
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.py

Run Flask

运行Flask

bash
undefined
bash
undefined

Development

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
undefined
uv run flask run --host=0.0.0.0 --port=8080
undefined

Project 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
undefined
bash
undefined

Linting 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
undefined
uv run pytest uv run pytest -v --cov=src
undefined

Scripts

脚本

  • scripts/init-project.sh
    - Initialize new Python project with standard structure
  • scripts/setup-flask.sh
    - Set up Flask application boilerplate
  • scripts/init-project.sh
    - 初始化带有标准结构的新Python项目
  • scripts/setup-flask.sh
    - 搭建Flask应用模板代码

References

参考资料

  • See
    references/uv-commands.md
    for complete uv reference
  • See
    references/flask-patterns.md
    for Flask best practices
  • 完整uv命令参考请查看
    references/uv-commands.md
  • Flask最佳实践请查看
    references/flask-patterns.md