Explore Python Libraries: Python Fire
Jul 28, 2020 • 9 Minute Read
Introduction
In this guide, you will see how to implement Python/Google Fire. It is an open-source library that is developed and maintained by Google and it is used most commonly to make command line interfaces out of Python code. In this guide, you will also learn how to use the interactive mode of fire module.
What is Python Fire?
Python Fire is a module from which you can convert your Python code into a command-line interface with just a single call. It is such an amazing library that it can turn any Python component into the command line interface easily. It can also convert a module into a command line interface without even knowing its source code.
It is called Fire because when you call Fire, it fires off (executes) your command.
Benefits of Python Fire
- You can easily create command line interfaces.
- It helps greatly in debugging Python code.
- It is such a wonderful library that you can use it to generate command line interfaces from any Python library.
- You can call a module without knowing its source code.
- It helps greatly to explore and dig deep down in your code.
Installation of Python Fire
There are no dependencies for installing Python Fire, assuming that you have Python and pip installed on your system.
To install using pip, open the terminal and run the following command:
pip install fire
To install using conda, open the terminal and run the following command:
conda install -c conda-forge fire
Basic Examples
Example One: fire.fire()
Now that you have installed the Fire package, take a look at this first example.
# example.py
# importing python fire module
import fire
# defining a function
def hello(name):
return 'Hello {0}, Have a nice day {0}'.format(name)
if __name__ == '__main__':
# converting our function in a Command Line Interface (CLI).
fire.Fire()
To run the above code as a command line interface use the below command:
python example.py hello john
Example Two: fire.fire(function)
Modify your function so that you don't need to specify your whole function.
# example.py
# importing python fire module
import fire
# defining a function
def hello(name):
return 'Hello {0}, Have a nice day {0}'.format(name)
if __name__ == '__main__':
# converting our function in a Command Line Interface (CLI).
fire.Fire(hello)
Run the below command over the terminal to run the program:
python example.py sachin
Example Three: Making Fire Simpler
# example.py
# importing fire
import fire
name1 = 'Hello my name is sachin'
name2 = 'hello my name is rahul'
fire.Fire()
To run the above code you can use following commands
python example.py name1
python example.py name2
Multiple Commands Examples
Example One: Basic Example
# example.py
# importing fire module
import fire
def hello(name):
return 'Hello how are you {}'.format(name)
def nice(name):
return 'nice day thank you! {}'.format(name)
def bye(name):
return 'bye take care! {}'.format(name)
# converting our function in a Command Line Interface (CLI).
if __name__ == '__main__':
fire.Fire()
Run the below commands over the terminal to run the program:
python example.py hello sachin
python example.py nice sachin
python example.py bye sachin
Example Two: Dictionary
The previous example exposed all functions to the command lines. This example will introduce the dictionary. It uses the same functions such as hello, nice, and bye.
# converting our function in a Command Line Interface (CLI).
if __name__ == '__main__':
fire.Fire({
'hello' : hello,
'nice': nice,
'bye' : bye
})
By using a dictionary you can selectively expose the functions to the command line.
Run the below commands over the terminal to run the program:
python example.py hello sachin
python example.py nice sachin
python example.py bye sachin
Example Three: Objects
In Python Fire you can also work with objects. It's a great way to expose a function to a command line.
# importing fire module
import fire
class World(object):
def hello(self,name):
return 'Hello how are you {}'.format(name)
def nice(self,name):
return 'nice day thank you! {}'.format(name)
def bye(self,name):
return 'bye take care! {}'.format(name)
# converting our function in a Command Line Interface (CLI).
if __name__ == '__main__':
world_instance = World()
fire.Fire(world_instance)
You can run the above example by the same command.
python example.py hello sachin
python example.py nice sachin
python example.py bye sachin
Example Four: Classes
Python Fire also provides the option to work with classes. You have used the same World class.
# converting our function in a Command Line Interface (CLI).
if __name__ == '__main__':
fire.Fire(World)
To run the above code you can use the commands used in Example One.
Fire Flags
In Python Fire CLIs a number of flags are provided. To use these flags in a command line prompt they must be separated by the fire command by an isolated --. If there is at least one isolated -- argument, then arguments after the final isolated -- are treated as flags, whereas all arguments before the final isolated -- are considered part of the fire command.
There are some useful flags like the --help flag and --interactive flag. By using --help you can know the function's name, whether it's a positional argument, etc. You can use the --interactive flag on any CLI to enter a Python REPL with all the modules and variables used in the context where fire was called already and available to you for use. Other useful variables like --, the component, result, and trace of the fire command will also be available.
Run these flags on the below code.
# importing python fire module
import fire
# defining a function
def hello(name):
return 'Hello {0}, Have a nice day {0}'.format(name)
if __name__ == '__main__':
# converting our fucntion in a Command Line Interface (CLI).
fire.Fire(hello)
Use the below command to run the flags on the above code:
Help Flag
python example.py -- --help
Interactive Flag
Use the below command to run the interactive flag.
python example.py hello -- --interactive
Note: This would also open the Python interactive console on your terminal. You can execute or run these objects over that console.
Conclusion
In this guide, you saw the basics of Python Fire. You saw how to expose multiple commands on command line interfaces. You learned different types of exposing techniques like how to expose classes, functions, objects, etc. Last, you saw fire flags that may come in handy when you are going to debug your code using Python.
I hope that through this guide I was able to help you understand the basics of Python Fire and motivate you to dwell deeper in this wonderful command-line interface creation tool. For a deeper understanding, you can always follow Python Fire Documentation.
For creating and sharing beautiful images of your source code, use Carbon.