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
Move Exceptions and Entry-points docs from reST to MyST (#3142)Rowlando13before adoption7f7bbe4
lines 1–88why, one step back1# Packaging Entry Points23```{eval-rst}4.. currentmodule:: click5```67It's recommended to write command line utilities as installable packages with8entry points instead of telling users to run ``python hello.py``.910A distribution package is a ``.whl`` file you install with pip or another Python11installer. You use a ``pyproject.toml`` file to describe the project and how it12is built into a package. You might upload this package to PyPI, or distribute it13to your users in another way.1415Python installers create executable scripts that will run a specified Python16function. These are known as "entry points". The installer knows how to create17an executable regardless of the operating system, so it will work on Linux,18Windows, MacOS, etc.1920## Project Files2122To install your app with an entry point, all you need is the script and a23``pyproject.toml`` file. Here's an example project directory:2425```text26hello-project/27 src/28 hello/29 __init__.py30 hello.py31 pyproject.toml32```3334Contents of ``hello.py``:3536```{eval-rst}37.. click:example::38 import click3940 @click.command()41 def cli():42 """Prints a greeting."""43 click.echo("Hello, World!")44```4546Contents of ``pyproject.toml``:4748```toml49[project]50name = "hello"51version = "1.0.0"52description = "Hello CLI"53requires-python = ">=3.11"54dependencies = [55 "click>=8.1",56]5758[project.scripts]59hello = "hello.hello:cli"6061[build-system]62requires = ["flit_core<4"]63build-backend = "flit_core.buildapi"64```6566The magic is in the ``project.scripts`` section. Each line identifies one executable67script. The first part before the equals sign (``=``) is the name of the script that68should be generated, the second part is the import path followed by a colon69(``:``) with the function to call (the Click command).7071## Installation7273When your package is installed, the installer will create an executable script74based on the configuration. During development, you can install in editable75mode using the ``-e`` option. Remember to use a virtual environment!7677```console78$ python -m venv .venv79$ . .venv/bin/activate80$ pip install -e .81```8283Afterwards, your command should be available:8485```console86$ hello87Hello, World!88```