python-scripting

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Python Scripting Skill

Python脚本编写技能

Creates self-contained Python scripts using uv and PEP 723 inline script metadata.
使用uv和PEP 723内联脚本元数据创建独立Python脚本。

What This Skill Does

该技能的功能

  • Creates standalone Python scripts
  • Uses PEP 723 inline dependencies
  • Sets up argument parsing
  • Handles input/output
  • Configures reproducible builds
  • 创建独立Python脚本
  • 使用PEP 723内联依赖
  • 配置参数解析
  • 处理输入/输出
  • 配置可复现构建

When to Use

适用场景

  • Standalone utility scripts
  • One-off automation tasks
  • Quick data processing
  • CLI tools
  • Scripts that need dependencies
  • 独立实用工具脚本
  • 一次性自动化任务
  • 快速数据处理
  • CLI工具
  • 需要依赖的脚本

Reference Files

参考文件

  • references/UV_SCRIPT.template.py
    - Python script template with PEP 723 metadata
  • references/UV_SCRIPT.template.py
    - 包含PEP 723元数据的Python脚本模板

PEP 723 Format

PEP 723格式

python
#!/usr/bin/env -S uv run --script
python
#!/usr/bin/env -S uv run --script

/// script

/// script

requires-python = ">=3.12"

requires-python = ">=3.12"

dependencies = [

dependencies = [

"requests",

"requests",

"rich",

"rich",

]

]

///

///

undefined
undefined

Running Scripts

运行脚本

bash
uv run script.py [args]
Dependencies install automatically on first run.
bash
uv run script.py [args]
首次运行时会自动安装依赖。

Best Practices

最佳实践

  • Use
    exclude-newer
    for reproducibility
  • Include docstring with usage examples
  • Use argparse for CLI arguments
  • Return exit codes (0 success, non-zero error)
  • Keep scripts focused on one task
  • 使用
    exclude-newer
    确保可复现性
  • 包含带有使用示例的文档字符串
  • 使用argparse处理CLI参数
  • 返回退出码(0表示成功,非零表示错误)
  • 保持脚本专注于单一任务