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.
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.
It helps greatly to explore and dig deep down in your code.
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:
1pip install fire
To install using conda
, open the terminal and run the following command:
1conda install -c conda-forge fire
Now that you have installed the Fire package, take a look at this first example.
1# example.py
2# importing python fire module
3import fire
4
5# defining a function
6def hello(name):
7 return 'Hello {0}, Have a nice day {0}'.format(name)
8
9if __name__ == '__main__':
10 # converting our function in a Command Line Interface (CLI).
11 fire.Fire()
To run the above code as a command line interface use the below command:
1python example.py hello john
Modify your function so that you don't need to specify your whole function.
1# example.py
2# importing python fire module
3import fire
4
5# defining a function
6def hello(name):
7 return 'Hello {0}, Have a nice day {0}'.format(name)
8
9if __name__ == '__main__':
10 # converting our function in a Command Line Interface (CLI).
11 fire.Fire(hello)
Run the below command over the terminal to run the program:
1python example.py sachin
1# example.py
2# importing fire
3import fire
4
5name1 = 'Hello my name is sachin'
6name2 = 'hello my name is rahul'
7fire.Fire()
To run the above code you can use following commands
1python example.py name1
2python example.py name2
1# example.py
2# importing fire module
3import fire
4
5def hello(name):
6 return 'Hello how are you {}'.format(name)
7
8def nice(name):
9 return 'nice day thank you! {}'.format(name)
10
11def bye(name):
12 return 'bye take care! {}'.format(name)
13
14# converting our function in a Command Line Interface (CLI).
15if __name__ == '__main__':
16 fire.Fire()
Run the below commands over the terminal to run the program:
1python example.py hello sachin
2python example.py nice sachin
3python example.py bye sachin
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
.
1# converting our function in a Command Line Interface (CLI).
2if __name__ == '__main__':
3 fire.Fire({
4 'hello' : hello,
5 'nice': nice,
6 'bye' : bye
7 })
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:
1python example.py hello sachin
2python example.py nice sachin
3python example.py bye sachin
In Python Fire you can also work with objects. It's a great way to expose a function to a command line.
1# importing fire module
2import fire
3
4class World(object):
5 def hello(self,name):
6 return 'Hello how are you {}'.format(name)
7
8 def nice(self,name):
9 return 'nice day thank you! {}'.format(name)
10
11 def bye(self,name):
12 return 'bye take care! {}'.format(name)
13
14# converting our function in a Command Line Interface (CLI).
15if __name__ == '__main__':
16 world_instance = World()
17 fire.Fire(world_instance)
You can run the above example by the same command.
1python example.py hello sachin
2python example.py nice sachin
3python example.py bye sachin
Python Fire also provides the option to work with classes. You have used the same World
class.
1# converting our function in a Command Line Interface (CLI).
2if __name__ == '__main__':
3 fire.Fire(World)
To run the above code you can use the commands used in Example One.
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.
1# importing python fire module
2import fire
3
4# defining a function
5def hello(name):
6 return 'Hello {0}, Have a nice day {0}'.format(name)
7
8if __name__ == '__main__':
9 # converting our fucntion in a Command Line Interface (CLI).
10 fire.Fire(hello)
Use the below command to run the flags on the above code:
1python example.py -- --help
Use the below command to run the interactive flag.
1python 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.
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.