entry-points.md
This file
Path
docs/entry-points.md
88 lines · 2.2 KiB · Markdown
Last changed by
Move Exceptions and Entry-points docs from reST to MyST (#3142)Rowlando13before adoption7f7bbe4
History of this fileTake it
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889# Packaging Entry Points
```{eval-rst}.. currentmodule:: click```
It's recommended to write command line utilities as installable packages withentry points instead of telling users to run ``python hello.py``.
A distribution package is a ``.whl`` file you install with pip or another Pythoninstaller. You use a ``pyproject.toml`` file to describe the project and how itis built into a package. You might upload this package to PyPI, or distribute itto your users in another way.
Python installers create executable scripts that will run a specified Pythonfunction. These are known as "entry points". The installer knows how to createan executable regardless of the operating system, so it will work on Linux,Windows, MacOS, etc.
## Project Files
To install your app with an entry point, all you need is the script and a``pyproject.toml`` file. Here's an example project directory:
```texthello-project/ src/ hello/ __init__.py hello.py pyproject.toml```
Contents of ``hello.py``:
```{eval-rst}.. click:example:: import click
@click.command() def cli(): """Prints a greeting.""" click.echo("Hello, World!")```
Contents of ``pyproject.toml``:
```toml[project]name = "hello"version = "1.0.0"description = "Hello CLI"requires-python = ">=3.11"dependencies = [ "click>=8.1",]
[project.scripts]hello = "hello.hello:cli"
[build-system]requires = ["flit_core<4"]build-backend = "flit_core.buildapi"```
The magic is in the ``project.scripts`` section. Each line identifies one executablescript. The first part before the equals sign (``=``) is the name of the script thatshould be generated, the second part is the import path followed by a colon(``:``) with the function to call (the Click command).
## Installation
When your package is installed, the installer will create an executable scriptbased on the configuration. During development, you can install in editablemode using the ``-e`` option. Remember to use a virtual environment!
```console$ python -m venv .venv$ . .venv/bin/activate$ pip install -e .```
Afterwards, your command should be available:
```console$ helloHello, World!```