Python Fire
Python Fire is a library for automatically generating command-line interfaces (CLIs) from any Python object. It enables developers to turn functions, classes, objects, and dictionaries into CLIs with minimal code, simplifying the creation of powerful command-line tools. The current version is 0.7.1, with a consistent release cadence focusing on compatibility, feature enhancements, and bug fixes.
Common errors
-
ModuleNotFoundError: No module named 'fire'
cause The 'fire' library has not been installed in your Python environment or the wrong Python interpreter is being used.fixEnsure you are using the correct Python environment and install the library using pip: `pip install fire` -
AttributeError: module 'fire' has no attribute 'Fire'
cause This error typically occurs when your Python script file is named 'fire.py', which causes a naming conflict with the installed `fire` package. When you try to `import fire`, Python imports your local script instead of the library.fixRename your Python script file to something other than 'fire.py' (e.g., 'my_cli.py') to avoid the module name conflict. -
ERROR: Could not consume arg: <argument_name>
cause Python Fire did not correctly parse the command-line arguments, often due to arguments containing spaces not being properly quoted, or an unexpected argument structure.fixEnsure arguments with spaces are enclosed in quotes (e.g., `python my_script.py say_hello 'John Doe'`). For flags and values, use the `--` separator to distinguish Fire's internal flags from your command's arguments, or explicitly use flag syntax (e.g., `python my_script.py my_function --arg_name=value`). -
TypeError: <function_name>() missing 1 required positional argument: '<argument_name>'
cause A required argument for a function exposed via Python Fire was not provided in the command-line call.fixProvide all necessary arguments to the function when calling it from the command line. For example, if a function `greet(name)` is fired, call it as `python your_script.py greet --name='World'` or `python your_script.py greet World`.
Warnings
- breaking Python Fire dropped support for Python 2 starting with version 0.7.0. If your project relies on Python 2, you must use an older version of the library.
- gotcha Arguments passed to a Fire-generated CLI that start with `--` (e.g., `--my-flag`) might not have been correctly interpreted as strings in very old versions of Fire. This could lead to unexpected parsing errors.
- gotcha While `fire.Fire()` is typically called directly within a script, you can also use `python -m fire <module_name>` or `python -m fire <path_to_file.py>` to generate a CLI for an existing module or file without modifying its source code. This is useful for exploring or interacting with third-party libraries.
Install
-
pip install fire
Imports
- Fire
from fire import Fire
Quickstart
import fire
def greet(name='World', enthusiasm=1):
"""Greets the given name with specified enthusiasm."""
return f"Hello {name}{'!' * enthusiasm}"
if __name__ == '__main__':
fire.Fire(greet)