Sub topic 14
Installing the tool as a command
This chapter assumes you can write a main(argv=None) function that returns an exit code, and that you know how to create a virtual environment and run pip. It turns a script you run with python greet.py into a command you can type as greet.
Why install a tool
Running python greet.py Ada works only from the folder that holds the file. An installed command works from any directory and appears on your PATH, the list of folders your shell searches for programs. It also lets other people install your tool with one pip command.
Python does this with an entry point. An entry point is a name in your package metadata that points to a function. When you install the package, the installer creates a small launcher script with that name.
Declaring the entry point
Put the entry point in pyproject.toml, next to your module greet.py:
[project]
name = "greet"
version = "0.1.0"
[project.scripts]
greet = "greet:main"The line greet = "greet:main" has three parts:
- The name on the left,
greet, is the command the user types. - The module on the right, before the colon, is
greet, which maps togreet.py. - The function after the colon,
main, is what the launcher calls.
The launcher runs roughly sys.exit(main()). This is why main should return an integer. A return value of 0 becomes exit status 0, and any other integer becomes that exit status. If main returns None, the exit status is also 0.
A build backend
A pyproject.toml that has only [project] is incomplete for a single-file module. You also need a [build-system] table that names a build backend, the tool that packages your code. Setuptools is a common choice, and it needs to be told which module to include:
[build-system]
requires = ["setuptools>=61"]
build-backend = "setuptools.build_meta"
[project]
name = "greet"
version = "0.1.0"
[project.scripts]
greet = "greet:main"
[tool.setuptools]
py-modules = ["greet"]The py-modules line lists single-file modules. If your code lives in a package directory, use packages or automatic discovery instead.
Installing and trying it
Create a virtual environment, then install your project in editable mode:
python -m venv .venv
source .venv/bin/activate
pip install -e .
greet Ada --count 2On Windows, activate with .venv\Scripts\activate. Editable mode (-e) links the installed command to your source files. When you edit greet.py, the command picks up the change without a reinstall. Changes to pyproject.toml, such as a new entry point, still need another pip install -e ..
Common errors
| Symptom | Likely cause |
|---|---|
| `greet: command not found` | The virtual environment is not active, or the install failed. |
| `ModuleNotFoundError` when running the command | The module is not listed in `py-modules` or `packages`. |
| The command runs old behavior after you changed the entry point | You edited `pyproject.toml` but did not reinstall. |
| Exit status is always 0 | `main` returns `None`, or the entry point points to a different function. |
Exercise
You add [project.scripts] with tool = "mytool.cli:run". Where must the function run be defined, and what should it return so the command exits with status 1 on failure?
Show answer
run must be a function in the module cli inside the package mytool (the file mytool/cli.py). It should return the integer 1 on failure, because the launcher passes the return value to sys.exit. It returns 0 on success.
Exercise
You change the version field in pyproject.toml and edit greet.py. After an editable install, which change takes effect immediately?
Show answer
The edit to greet.py takes effect immediately, because editable mode links to the source file. The version change is package metadata, so you must run pip install -e . again to update it.