Skip to content
Myr

entry-points.md

This file

Path

docs/entry-points.md

88 lines · 2.2 KiB · Markdown

Last changed by

[pre-commit.ci lite] apply automatic fixespre-commit-ci-lite[bot]before adoption1a1cdcb

History of this file

Take it

switch format from rest to mystdenuowebbefore adoption8677596

line 1why, one step back
1# Packaging Entry Points

rename extension from .rst to .mddenuowebbefore adoptiond1d3538

line 2why, one step back
2

switch format from rest to mystdenuowebbefore adoption8677596

lines 3–6why, one step back
3```{eval-rst}
4.. currentmodule:: click5```6

rename extension from .rst to .mddenuowebbefore adoptiond1d3538

lines 7–19why, one step back
7It's recommended to write command line utilities as installable packages with
8entry 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.19

switch format from rest to mystdenuowebbefore adoption8677596

line 20why, one step back
20## Project Files

rename extension from .rst to .mddenuowebbefore adoptiond1d3538

lines 21–24why, one step back
21
22To install your app with an entry point, all you need is the script and a23``pyproject.toml`` file. Here's an example project directory:24

switch format from rest to mystdenuowebbefore adoption8677596

lines 25–32why, one step back
25```text
26hello-project/27 src/28 hello/29 __init__.py30 hello.py31 pyproject.toml32```

rename extension from .rst to .mddenuowebbefore adoptiond1d3538

lines 33–35why, one step back
33
34Contents of ``hello.py``:35

switch format from rest to mystdenuowebbefore adoption8677596

line 36why, one step back
36```{eval-rst}

rename extension from .rst to .mddenuowebbefore adoptiond1d3538

lines 37–43why, one step back
37.. click:example::
38 import click3940 @click.command()41 def cli():42 """Prints a greeting."""43 click.echo("Hello, World!")

switch format from rest to mystdenuowebbefore adoption8677596

line 44why, one step back
44```

rename extension from .rst to .mddenuowebbefore adoptiond1d3538

lines 45–47why, one step back
45
46Contents of ``pyproject.toml``:47

switch format from rest to mystdenuowebbefore adoption8677596

lines 48–56why, one step back
48```toml
49[project]50name = "hello"51version = "1.0.0"52description = "Hello CLI"53requires-python = ">=3.11"54dependencies = [55 "click>=8.1",56]

rename extension from .rst to .mddenuowebbefore adoptiond1d3538

line 57why, one step back
57

switch format from rest to mystdenuowebbefore adoption8677596

lines 58–59why, one step back
58[project.scripts]
59hello = "hello.hello:cli"

rename extension from .rst to .mddenuowebbefore adoptiond1d3538

line 60why, one step back
60

switch format from rest to mystdenuowebbefore adoption8677596

lines 61–64why, one step back
61[build-system]
62requires = ["flit_core<4"]63build-backend = "flit_core.buildapi"64```

rename extension from .rst to .mddenuowebbefore adoptiond1d3538

lines 65–70why, one step back
65
66The 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).70

switch format from rest to mystdenuowebbefore adoption8677596

line 71why, one step back
71## Installation

rename extension from .rst to .mddenuowebbefore adoptiond1d3538

lines 72–76why, one step back
72
73When 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!76

switch format from rest to mystdenuowebbefore adoption8677596

lines 77–81why, one step back
77```console
78$ python -m venv .venv79$ . .venv/bin/activate80$ pip install -e .81```

rename extension from .rst to .mddenuowebbefore adoptiond1d3538

lines 82–84why, one step back
82
83Afterwards, your command should be available:84

switch format from rest to mystdenuowebbefore adoption8677596

lines 85–88why, one step back
85```console
86$ hello87Hello, World!88```