Skip to content

Commit

Permalink
Expand entry points documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
zanieb committed Nov 21, 2024
1 parent 8149e63 commit 42d58b4
Showing 1 changed file with 67 additions and 7 deletions.
74 changes: 67 additions & 7 deletions docs/concepts/projects/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,80 @@ 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"
```

!!! important
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`.

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"
```

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.

### 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

Using `[project.scripts]` requires a [build system](#build-systems) to be defined.
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 42d58b4

Please sign in to comment.