如何运行 Python 脚本¶
run
命令支持运行带有 内联元数据 的 Python 脚本,此类脚本会自动创建一个专用的 环境,并安装所需依赖项,同时使用合适的 Python 版本。
脚本元数据块是以 # /// script
开始、以 # ///
结束的注释块。两者之间的每一行都必须是以 #
开头的注释,并且在去除注释字符后,必须是一个合法的 TOML 文档。
支持的顶层字段包括:
以下是一个带有合法元数据块的 Python 脚本示例:
# /// script
# requires-python = ">=3.11"
# dependencies = [
# "httpx",
# "rich",
# ]
# ///
import httpx
from rich.pretty import pprint
resp = httpx.get("https://peps.python.org/api/peps.json")
data = resp.json()
pprint([(k, v["title"]) for k, v in data.items()][:10])
直接运行它:
$ hatch run /path/to/script.py
Creating environment: SyB4bPbL
Checking dependencies
Syncing dependencies
[
│ ('1', 'PEP Purpose and Guidelines'),
│ ('2', 'Procedure for Adding New Modules'),
│ ('3', 'Guidelines for Handling Bug Reports'),
│ ('4', 'Deprecation of Standard Modules'),
│ ('5', 'Guidelines for Language Evolution'),
│ ('6', 'Bug Fix Releases'),
│ ('7', 'Style Guide for C Code'),
│ ('8', 'Style Guide for Python Code'),
│ ('9', 'Sample Plaintext PEP Template'),
│ ('10', 'Voting Guidelines')
]
说明
- 示例中显示的提示信息仅会在首次运行时临时出现在终端中。
- 尽管环境名称基于脚本的绝对路径生成,但命令行参数不要求使用绝对路径。
环境配置¶
您也可以通过 [tool.hatch]
表来直接控制脚本的 环境。例如,如果希望禁用默认启用的 UV 安装器(见 启用 UV),可添加如下配置:
# /// script
# ...
# [tool.hatch]
# installer = "pip"
# ///