<p align="center">
<picture>
<source media="(prefers-color-scheme: dark)" srcset="https://brentyi.github.io/tyro/_static/logo-dark.svg" />
<img alt="tyro logo" src="https://yellow-cdn.veclightyear.com/ab5030c0/002b8d2a-4742-4b09-8922-1f170689f796.svg" width="200px" />
</picture>
</p>
<p align="center">
<em><a href="https://brentyi.github.io/tyro">文档</a></em>
•
<em><code>pip install tyro</code></em>
</p>
<p align="center">
<img alt="build" src="https://yellow-cdn.veclightyear.com/ab5030c0/33b89f66-2bfd-4712-9a27-4dc0b6f03225.svg" />
<img alt="mypy" src="https://yellow-cdn.veclightyear.com/ab5030c0/abdd73b5-8c9d-4dbb-9db8-417594b15fc6.svg" />
<img alt="pyright" src="https://yellow-cdn.veclightyear.com/ab5030c0/57c31811-ea89-4784-a183-a79f3f1e53af.svg" />
<img alt="ruff" src="https://yellow-cdn.veclightyear.com/ab5030c0/910d55c2-d7c3-4808-b675-d82903632e24.svg" />
<a href="https://codecov.io/gh/brentyi/tyro">
<img alt="codecov" src="https://yellow-cdn.veclightyear.com/ab5030c0/25e7b03b-e452-4ec6-a721-8a20f71b005b.svg" />
</a>
<a href="https://pypi.org/project/tyro/">
<img alt="codecov" src="https://img.shields.io/pypi/pyversions/tyro" />
</a>
</p>
<br />
<strong><code>tyro.cli()</code></strong> 通过 Python 类型内省生成命令行界面。我们可以使用函数定义可配置的脚本:
"""使用函数签名定义的命令行界面。
用法: python script_name.py --foo INT [--bar STR]
"""
import tyro
def main(
foo: int,
bar: str = "default",
) -> None:
... # 脚本的主体。
if __name__ == "__main__":
# 生成CLI并使用foo和bar两个参数调用main。
tyro.cli(main)
或使用dataclasses 、pydantic和attrs等工具定义配置对象:
"""使用类签名定义的命令行界面。
用法: python script_name.py --foo INT [--bar STR]
"""
from dataclasses import dataclass
import tyro
@dataclass
class Config:
foo: int
bar: str = "default"
if __name__ == "__main__":
# 生成CLI并使用foo和bar两个参数实例化Config。
config = tyro.cli(Config)
# 脚本其余部分。
assert isinstance(config, Config) # 应该通过。
其他功能包括帮助文本生成、嵌套结构、shell补全和子命令。有关示例和API参考,请参见我们的文档。
实际应用
tyro
设计为轻量级,适用于临时脚本,同时也可用于大型项目,提供类型安全和模块化。例子:
<table>
<tr>
<td>
<a href="https://github.com/nerfstudio-project/nerfstudio/">
nerfstudio-project/nerfstudio
<br /><img
alt="GitHub star count"
src="https://img.shields.io/github/stars/nerfstudio-project/nerfstudio?style=social"
/>
</a>
</td>
<td>
用于神经辐射场的开源工具。
</td>
</tr>
<tr>
<td>
<a href="https://github.com/Sea-Snell/JAXSeq/">
Sea-Snell/JAXSeq
<br /><img
alt="GitHub star count"
src="https://img.shields.io/github/stars/Sea-Snell/JAXSeq?style=social"
/>
</a>
</td>
<td>在Jax中训练非常大的语言模型。</td>
</tr>
<tr>
<td>
<a href="https://github.com/kevinzakka/obj2mjcf">
kevinzakka/obj2mjcf
<br /><img
alt="GitHub star count"
src="https://img.shields.io/github/stars/kevinzakka/obj2mjcf?style=social"
/>
</a>
</td>
<td>用于处理Mujoco OBJ文件的接口。</td>
</tr>
<tr>
<td>
<a href="https://github.com/blurgyy/jaxngp">
blurgyy/jaxngp
<br /><img
alt="GitHub star count"
src="https://img.shields.io/github/stars/blurgyy/jaxngp?style=social"
/>
</a>
</td>
<td>
基于JAX的<a href="https://nvlabs.github.io/instant-ngp/">instant-ngp</a>的CUDA加速实现。
</td>
</tr>
<tr>
<td>
<a href="https://github.com/NVIDIAGameWorks/kaolin-wisp">
NVIDIAGameWorks/kaolin-wisp
<br /><img
alt="GitHub star count"
src="https://img.shields.io/github/stars/NVIDIAGameWorks/kaolin-wisp?style=social"
/>
</a>
</td>
<td>用于神经场的PyTorch库。</td>
</tr>
<tr>
<td>
<a href="https://github.com/autonomousvision/sdfstudio">
autonomousvision/sdfstudio
<br /><img
alt="GitHub star count"
src="https://img.shields.io/github/stars/autonomousvision/sdfstudio?style=social"
/>
</a>
</td>
<td>用于表面重建的统一框架。</td>
</tr>
<tr>
<td>
<a href="https://github.com/openrlbenchmark/openrlbenchmark">
openrlbenchmark/openrlbenchmark
<br /><img
alt="GitHub star count"
src="https://img.shields.io/github/stars/openrlbenchmark/openrlbenchmark?style=social"
/>
</a>
</td>
<td>用于强化学习的跟踪实验集合。</td>
</tr>
<tr>
<td>
<a href="https://github.com/vwxyzjn/cleanrl">
vwxyzjn/cleanrl
<br /><img
alt="GitHub star count"
src="https://img.shields.io/github/stars/vwxyzjn/cleanrl?style=social"
/>
</a>
</td>
<td>深度强化学习算法的单文件实现。</td>
</tr>
</table>
替代方案
tyro
是一个比较特定的库。如果我们的设计决策不合理,欢迎提交issue。
您也可以考虑使用其他库,我们特别喜欢以下几个:
我们还在文档中对tyro
的设计目标和其他替代方案做了一些说明,点击这里查看。