Tutorials  /  Python

How to Create a CLI with Python Fire on Ubuntu 22.04

Ccentron Redaktion · January 2025 ·4 min read ·Python, Tutorial

Introduction to Create a CLI

Python Fire is a Python library that lets you automatically create CLIs (command line interfaces) from Python objects. CLIs are programs that take text input from the command line and can be helpful for developing and debugging workflows. This can be especially useful for those comfortable working in Bash and the command line.

While the traditional method of using argparse from the standard Python library can achieve similar results, Python Fire improves prototyping and maintenance. If you need complete control, check out the tutorial on how to use argparse to write command line programs in Python.

In this tutorial, you will install Python Fire and use it to create custom CLIs. These CLIs can be created without modifying Python source code or by integrating Python Fire directly into your functions and classes for more granular control.

VM

Matching infrastructure at centron

Scripts and services eventually need an environment that keeps running: ccloud³ VMs from €3.12 per month, billed by the hour. Rent a cloud server →

Prerequisites to Create a CLI

  • An Ubuntu 22.04 server set up with a non-root user with sudo privileges and a firewall enabled.
  • The latest version of Python 3 installed on your machine with a virtual environment created.
  • Familiarity with Python programming. A good primer is this series on how to code in Python 3.

Step 1 — Installing Python Fire

Python Fire is hosted on PyPI and is installable via pip. Ensure your virtual environment is activated:

Console
cd environments
source my_env/bin/activate
pip install fire

This installation ensures no interference with global pip installations, which might conflict with package managers like apt.

Step 2 — Create a CLI from a Program without Code Additions

Create a new Python file:

Code
nano example.py

Add the following code:

Python
def my_name(name):
  return "My name is {name}".format(name=name)

Save and exit the file. To execute this as a CLI, run:

Console
python -m fire example my_name --name=sammy

Output:

Code
My name is sammy

Step 3 — Create a CLI with Python Fire Imported

Edit the file to include Python Fire:

Python
import fire

def my_name(name):
  return "My name is {name}".format(name=name)

if __name__ == '__main__':
  fire.Fire()

Now you can run the CLI with:

Console
python example.py my_name sammy

Output:

Code
My name is sammy

Step 4 — Create a CLI from a Function

Update the code to specify the function:

Python
import fire

def my_name(name):
  return "My name is {name}".format(name=name)

if __name__ == '__main__':
  fire.Fire(my_name)

Run:

Console
python example.py sammy

Output:

Code
My name is sammy

Step 5 — Create a CLI from a Class

Refactor your code into a class:

Python
import fire

class Namer(object):
    def my_name(self, name):
        return "My name is {name}".format(name=name)

if __name__ == '__main__':
  fire.Fire(Namer)

Use the CLI:

Console
python example.py my_name sammy

Output:

Code
My name is sammy

Conclusion

In this tutorial, you installed Python Fire and explored various ways to create CLIs using Python programs, functions, and classes. For a deeper dive, refer to the official documentation or explore how to use argparse for creating command line programs in Python.

Jetzt 200 € Guthaben sichern

Testen Sie Ihr Setup auf ccloud³

Registrieren Sie sich in der ccloud³ und erhalten Sie 200 € Startguthaben für Ihr Projekt – z. B. für eine PostgreSQL-VM mit automatischen Backups.

centron Redaktion Technische Redaktion

Das Redaktionsteam von centron schreibt Anleitungen aus dem Betriebsalltag: getestet auf unserer eigenen Plattform, betrieben im Rechenzentrum in Hallstadt bei Bamberg.

Kategorie Python
Teilen
Noch offene Fragen?

Unser Team hilft Ihnen bei Ihrem konkreten Setup weiter – von Menschen, die die Plattform selbst betreiben.

War dieses Tutorial hilfreich?

Ihre Antwort wird anonym gespeichert und hilft uns, die Tutorials zu verbessern.

Kommentare

Noch keine Kommentare – stellen Sie die erste Frage zu diesem Tutorial.

Zum Kommentieren anmelden

Kommentare stehen centron-Kunden offen. Melden Sie sich in Ihrem Konto an, um eine Frage zu diesem Tutorial zu stellen.

Weiterlesen

Das könnte Sie auch interessieren

Jetzt kostenlos anfangen

Melden Sie sich an und erhalten Sie in den ersten 60 Tagen ein Guthaben von 200 € bei centron.

Dieses Werbeangebot gilt nur für neue Konten. Angebot ausschließlich für Gewerbetreibende.

Jetzt loslegen Sales kontaktieren