Python Fire:自动生成你的 CLI 程序


Python Fire 是用于从任意 Python 对象自动生成命令行接口(CLI)的库。

  • Python Fire 可简便地创建 CLI。 [1]
  • Python Fire 对开发与调试 Python 程序大有帮助。 [2]
  • Python Fire 有助于探索现有代码或将他人代码转为 CLI。 [3]
  • Python Fire 便利了 Bash 与 Python 间的转换。 [4]
  • Python Fire 通过用已导入或创建的模块及变量设置 Python REPL(交互式解释器)使其使用更简单。 [5]


To install Python Fire with pip, run: pip install fire

To install Python Fire with conda, run: conda install fire -c conda-forge

To install Python Fire from source, first clone the repository and then run: python install


你可以对任何 Python object 调用 Fire,比如:<br> functions、classes、modules、objects、dictionaries、lists、tuples 等等。 这都将运行!

这里有一个对 function 调用 Fire 的示例:

import fire

def hello(name="World"):
  return "Hello %s!" % name

if __name__ == '__main__':


python  # Hello World!
python --name=David  # Hello David!
python --help  # Shows usage information.

这里有一个对 class 调用 Fire 的示例:

import fire

class Calculator(object):
  """A simple calculator class."""

  def double(self, number):
    return 2 * number

if __name__ == '__main__':


python double 10  # 20
python double --number=15  # 30

想要学习 Fire 在 functions、objects、dicts、lists 等上的表现及其其他特性参见 Using a Fire CLI page.

想要查看更多的例子,参见The Python Fire Guide.

Why is it called Fire?

When you call Fire, it fires off (executes) your command.

Where can I learn more?

Please see The Python Fire Guide.


Setup Command Notes
install pip install fire
Creating a CLI Command Notes
import import fire
Call fire.Fire() Turns the current module into a Fire CLI.
Call fire.Fire(component) Turns component into a Fire CLI.
Using a CLI Command Notes
Help command --help or command -- --help
REPL command -- --interactive Enters interactive mode.
Separator command -- --separator=X Sets the separator to X. The default separator is -.
Completion command -- --completion [shell] Generates a completion script for the CLI.
Trace command -- --trace Gets a Fire trace for the command.
Verbose command -- --verbose

Note that these flags are separated from the Fire command by an isolated --.


Licensed under the Apache 2.0 License.


This is not an official Google product.

Sign In