A library for automatically generating command line interfaces from any Python object.
—
Fire provides interactive REPL functionality that allows users to drop into a Python shell with access to CLI components and execution context, making it valuable for debugging, exploration, and rapid prototyping.
Drop into a Python REPL with specified variables available in the local namespace.
def Embed(variables, verbose=False):
"""
Drop into Python REPL with variables available as local variables.
Automatically detects and uses IPython if available, otherwise falls back
to the built-in Python REPL using the code module's InteractiveConsole.
Parameters:
- variables: Dictionary of variables to make available (keys=names, values=objects)
- verbose: Whether to include 'hidden' members (names starting with _)
"""Using the --interactive flag:
import fire
class DataProcessor:
def __init__(self):
self.data = []
def load(self, filename):
# Load data from file
self.data = [1, 2, 3, 4, 5] # Example data
return f"Loaded {len(self.data)} items"
def process(self):
return [x * 2 for x in self.data]
if __name__ == '__main__':
fire.Fire(DataProcessor)
# Command line: python script.py load data.txt -- --interactive
# After loading, drops into REPL with processor instance availableProgrammatic interactive embedding:
import fire
from fire import interact
def analyze_data(data_file, algorithm='default'):
# Process data
data = load_data(data_file) # Hypothetical function
results = run_algorithm(data, algorithm) # Hypothetical function
# Drop into interactive mode for exploration
interact.Embed({
'data': data,
'results': results,
'algorithm': algorithm
})
return results
if __name__ == '__main__':
fire.Fire(analyze_data)Custom interactive session:
import fire
from fire import interact
class DebugTool:
def __init__(self):
self.debug_info = {}
def debug_session(self, context_name="debug"):
"""Start interactive debugging session."""
import sys
import traceback
# Gather debugging context
frame = sys._getframe(1)
local_vars = frame.f_locals
global_vars = frame.f_globals
debug_context = {
'locals': local_vars,
'globals': global_vars,
'traceback': traceback,
'sys': sys
}
print(f"Starting debug session: {context_name}")
interact.Embed(debug_context, verbose=True)
if __name__ == '__main__':
fire.Fire(DebugTool)Automatic variable detection:
When using Fire's --interactive flag, the REPL automatically includes:
IPython integration: If IPython is installed, Fire automatically uses it for enhanced interactive features:
Fallback to built-in REPL:
When IPython is not available, Fire uses Python's built-in code.InteractiveConsole with:
Debugging failed commands:
python my_cli.py problematic_command arg1 arg2 -- --interactive
# Examine variables and state when command failsExploring API interactively:
python my_cli.py -- --interactive
# Start with base component and explore methods/propertiesData science workflows:
import fire
from fire import interact
def load_and_explore(dataset_path):
import pandas as pd
df = pd.read_csv(dataset_path)
summary = df.describe()
# Drop into interactive mode for exploration
interact.Embed({
'df': df,
'summary': summary,
'pd': pd
})
return summary
if __name__ == '__main__':
fire.Fire(load_and_explore)
# python script.py data.csv
# Automatically drops into REPL with DataFrame availableThe interactive mode bridges the gap between command-line usage and Python REPL development, making Fire an excellent tool for both scripting and interactive data exploration.
Install with Tessl CLI
npx tessl i tessl/pypi-fire