# What are Python entry points?


When you install a Python package, you might get a new command to type in your terminal, or a new feature in a tool you already use, like pytest. An **entry point** tells the installer what function a command should run, or tells an app where to find a plugin.

## Turn a function into a command

Say your package has a `main()` function in `reportkit/cli.py` and you want people to run it by typing `report`. Add this to your `pyproject.toml`:

```toml {filename="pyproject.toml"}
[project.scripts]
report = "reportkit.cli:main"
```

After installation, typing `report` imports `reportkit.cli` and calls `main()` with no Python arguments. Installing the package sets up the command without calling the function.

```python {filename="reportkit/cli.py"}
def main():
    print("Report ready")
    return 0
```

With [uv](https://pydevtools.com/handbook/reference/uv.md), a configured {{< term "build-backend" >}}, and the package included in the build, run:

```bash
uv run report
```

Expect `Report ready` and exit code zero. `uv run` installs the project and dependencies into its virtual environment before executing the command. An [unpackaged project](https://pydevtools.com/handbook/explanation/understanding-uv-init-project-types.md) does not gain a command merely by declaring `[project.scripts]`.

The generated command passes the function’s return value to `sys.exit`. Return `0` or nothing to report success. Return a string to print an error message and report failure.

## Discover plugins without creating commands

Now suppose ReportKit lets other packages add export formats. A package that adds JSON export can list its function under the group ReportKit looks for:

```toml {filename="pyproject.toml"}
[project.entry-points."reportkit.exporters"]
json = "reportkit.exporters:export_json"
```

This doesn't create a `json` command. Instead, once the plugin package is installed in the same Python environment, ReportKit can find its entry point:

```python
from importlib.metadata import entry_points

for plugin in entry_points(group="reportkit.exporters"):
    print(plugin.name, plugin.value)
```

The loop prints `json reportkit.exporters:export_json`. Listing the entry points doesn’t import the plugin or check that its function exists. `plugin.load()` imports `reportkit.exporters` and returns its `export_json` object, so that implementation must exist. Importing the plugin can run its code, so load only plugins you trust.

The app decides what a plugin must provide and what to do if two plugins use the same name. For a real example, [pytest discovers plugins](https://pydevtools.com/handbook/explanation/essential-pytest-plugins.md) through the `pytest11` group.

## Which table should I use?

An entry point has a **group** (what it’s for), a **name** (what to call it), and an **object reference** (where the Python code lives). In [pyproject.toml](https://pydevtools.com/handbook/reference/pyproject.toml.md), the table determines the group:

| Project table | Group | Purpose |
|---|---|---|
| `[project.scripts]` | `console_scripts` | Create terminal commands |
| `[project.gui-scripts]` | `gui_scripts` | Create commands without a console on Windows |
| `[project.entry-points."myapp.exporters"]` | `myapp.exporters` | Tell an app where to find plugins |

## Reinstall after changing an entry point

Reinstall the package after you add, rename, or change an entry point. This applies even if an [editable install](https://pydevtools.com/handbook/explanation/what-is-an-editable-install.md) already reflects source changes.

An entry point also differs from `__main__.py`, which enables `python -m package`. Both can call the same function, but neither automatically defines the other.
