Skip to content

Commit

Permalink
Expand entry points documentation (#9329)
Browse files Browse the repository at this point in the history
  • Loading branch information
zanieb authored Nov 27, 2024
1 parent cc6bfa1 commit 6afb340
Showing 1 changed file with 68 additions and 6 deletions.
74 changes: 68 additions & 6 deletions docs/concepts/projects/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,82 @@ affects selection of dependency versions (they must support the same Python vers

## Entry points

Projects may define entry points for the project in the `[project.scripts]` table of the
`pyproject.toml`.
[Entry points](https://packaging.python.org/en/latest/specifications/entry-points/#entry-points) are
the official term for an installed package to advertise interfaces. These include:

For example, to declare a command called `hello` that invokes the `hello` function in the
`example_package_app` module:
- [Command line interfaces]()
- [Graphical user interfaces]()
- [Plugin entry points]()

!!! important

Using the entry point tables requires a [build system](#build-systems) to be defined.

### Command-line interfaces

Projects may define command line interfaces (CLIs) for the project in the `[project.scripts]` table
of the `pyproject.toml`.

For example, to declare a command called `hello` that invokes the `hello` function in the `example`
module:

```toml title="pyproject.toml"
[project.scripts]
hello = "example_package_app:hello"
hello = "example:hello"
```

Then, the command can be run from a console:

```console
$ uv run hello
```

### Graphical user interfaces

Projects may define graphical user interfaces (GUIs) for the project in the `[project.gui-scripts]`
table of the `pyproject.toml`.

!!! important

Using `[project.scripts]` requires a [build system](#build-systems) to be defined.
These are only different from [command-line interfaces](#command-line-interfaces) on Windows, where
they are wrapped by a GUI executable so they can be started without a console. On other platforms,
they behave the same.

For example, to declare a command called `hello` that invokes the `app` function in the `example`
module:

```toml title="pyproject.toml"
[project.gui-scripts]
hello = "example:app"
```

### Plugin entry points

Projects may define entry points for plugin discovery in the
[`\[project.entry-points\]`](https://packaging.python.org/en/latest/guides/creating-and-discovering-plugins/#using-package-metadata)
table of the `pyproject.toml`.

For example, to register the `example-plugin-a` package as a plugin for `example`:

```toml title="pyproject.toml"
[project.entry-points.'example.plugins']
a = "example_plugin_a"
```

Then, in `example`, plugins would be loaded with:

```python title="example/__init__.py"
from importlib.metadata import entry_points

for plugin in entry_points(group='example.plugins'):
plugin.load()
```

!!! note

The `group` key can be an arbitrary value, it does need to include the package name or
"plugins". However, is recommended to namespace the key by the package name to avoid collisions
with other packages.

## Build systems

Expand Down

0 comments on commit 6afb340

Please sign in to comment.