Sub topic 7
Exposing a command-line tool with entry points
This chapter assumes you can write a Python package, have a pyproject.toml with a [project] table, and know how to install a package into a virtual environment with pip.
What an entry point is
An entry point is a named pointer to a Python callable, stored in the package's metadata. The [project.scripts] table declares console scripts. Each key is the command name, and each value is module.path:function.
[project.scripts]
greeter = "greeter.cli:main"This line says: create a command called greeter that imports greeter.cli and calls main().
What the installer does
You do not write the launcher yourself. When pip installs the wheel, it generates a small executable in the environment's bin/ directory (Scripts\ on Windows). That executable imports your module, calls the function, and passes the function's return value to sys.exit().
The target function
The target must be callable with no arguments. Here is the function from the example project:
# src/greeter/cli.py
import sys
from greeter import greet
def main() -> None:
name = sys.argv[1] if len(sys.argv) > 1 else "world"
print(greet(name))main reads sys.argv itself, so the launcher does not need to pass anything in. Many projects use argparse or a library such as Click inside main instead of reading sys.argv directly.
After installing the package, test it:
python -m pip install .
greeter AdaThe output is Hello, Ada!.
Exit codes
The launcher calls sys.exit(main()). A function that returns None exits with status 0. To report failure, return an integer or call sys.exit(2) yourself.
def main() -> int:
if len(sys.argv) > 2:
print("usage: greeter [NAME]", file=sys.stderr)
return 2
print(greet(sys.argv[1] if len(sys.argv) > 1 else "world"))
return 0Common errors
| Symptom | Likely cause |
|---|---|
| `command not found: greeter` | The package was not installed, or the virtual environment is not active. |
| `ModuleNotFoundError` when running the command | The module path in the entry point is wrong. In a src layout, write `greeter.cli`, not `src.greeter.cli`. |
| `TypeError: main() missing 1 required positional argument` | The target function requires arguments. Give them defaults or read from `sys.argv`. |
| Changes to `pyproject.toml` have no effect | The launcher is generated at install time. Reinstall after editing the table. |
Other entry point groups
Console scripts are one case of a general mechanism. Tables named [project.entry-points."group.name"] let other programs discover plugins in your package. For example, pytest finds plugins through the pytest11 group. Use [project.gui-scripts] for graphical programs that should not open a console window on Windows.
Exercise
You declare mytool = "mytool.app:run" in [project.scripts], but run is defined as def run(path):. What happens when a user types mytool, and how do you fix it?
Show answer
The launcher calls run() with no arguments, so Python raises TypeError: run() missing 1 required positional argument: 'path'. Fix it by making run take no required arguments and read its input from sys.argv (or from argparse) inside the function.