Author avatar

Gaurav Singhal

Explore Python Libraries: Python Fire

Gaurav Singhal

  • Jul 28, 2020
  • 9 Min read
  • 185 Views
  • Jul 28, 2020
  • 9 Min read
  • 185 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:

1
pip install fire 
terminal

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

1
conda 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
2
3
4
5
6
7
8
9
10
11
# 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() 
python

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

1
python 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
2
3
4
5
6
7
8
9
10
11
# 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) 
python

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

1
python example.py sachin 
shell

example terminal execution

Example Three: Making Fire Simpler

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

To run the above code you can use following commands

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

example terminal execution

Multiple Commands Examples

Example One: Basic Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# 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() 
python

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

1
2
3
python example.py hello sachin 
python example.py nice sachin 
python 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
2
3
4
5
6
7
# converting our function in a Command Line Interface (CLI). 
if __name__ == '__main__': 
    fire.Fire({ 
        'hello' : hello, 
        'nice': nice, 
        'bye' : bye 
    }) 
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:

1
2
3
python example.py hello sachin 
python example.py nice sachin 
python 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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# 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) 
python

You can run the above example by the same command.

1
2
3
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.

1
2
3
# converting our function in a Command Line Interface (CLI). 
if __name__ == '__main__': 
    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
2
3
4
5
6
7
8
9
10
# 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) 
python

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

Help Flag

1
python example.py -- --help 
shell

example terminal execution

Interactive Flag

Use the below command to run the interactive flag.

1
python 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.

6