Author avatar

Gaurav Singhal

Explore Python Libraries: Python Fire

Gaurav Singhal

  • Jul 28, 2020
  • 9 Min read
  • 7,821 Views
  • Jul 28, 2020
  • 9 Min read
  • 7,821 Views
Data
Data Analytics
Machine Learning
Python

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:

1pip install fire 
terminal

To install using conda, open the terminal and run the following command:

1conda install -c conda-forge fire 
terminal

Basic Examples

Example One: fire.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() 
python

To run the above code as a command line interface use the below command:

1python example.py hello john 
shell

example terminal execution

Example Two: fire.fire(function)

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) 
python

Run the below command over the terminal to run the program:

1python example.py sachin 
shell

example terminal execution

Example Three: Making Fire Simpler

1# example.py 
2# importing fire 
3import fire 
4 
5name1 = 'Hello my name is sachin' 
6name2 = 'hello my name is rahul' 
7fire.Fire() 
python

To run the above code you can use following commands

1python example.py name1 
2python example.py name2 
shell

example terminal execution

Multiple Commands Examples

Example One: Basic Example

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() 
python

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 
shell

example terminal execution

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.

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    }) 
python

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 
shell

example terminal execution

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.

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) 
python

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 

Example Four: Classes

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) 
python

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.

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) 
python

Use the below command to run the flags on the above code:

Help Flag

1python example.py -- --help 
shell

example terminal execution

Interactive Flag

Use the below command to run the interactive flag.

1python example.py hello -- --interactive 
shell

example terminal execution

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.