Skip to article frontmatterSkip to article content

How to Define Scripts

Make your package do something!

Python makes it very easy to create CLI scripts. Let’s create a remove-arrow command for our arrow-to-knee package in src/arrow_to_knee/cli.py:

src/arrow_to_knee/cli.py
1
2
3
4
5
6
7
8
9
# This is an optional dependency, so importing
# _this_ package will fail if you don't have it
from rich import print as rprint


def remove_arrow():
    # Uses the rich library for formatting
    rprint("[italic red]It's stuck! Can't remove[/italic red]")
    raise SystemExit(1)

We can expose this as a CLI application by adding an entry in the project.scripts section:

pyproject.toml
1
2
[project.scripts]
remove-arrow = "arrow_to_knee.cli:remove_arrow"

This is defining a CLI application called remove-arrow implemented by the remove_arrow function defined in the arrow_to_knee.cli module. Let’s test it out:

$ remove-arrow
It's stuck! Can't remove