entry-points.rst
This file
Path
docs/entry-points.rst
86 lines · 2.2 KiB
Last changed by
switch to flit build backend (#2543)David Lordbefore adoption3fcf338
History of this fileTake it
switch to flit build backend (#2543)David Lordbefore adoption3fcf338
lines 1–86why, one step back1Packaging Entry Points2======================34It's recommended to write command line utilities as installable packages with5entry points instead telling users to run ``python hello.py``.67A distribution package is a ``.whl`` file you install with pip or another Python8installer. You use a ``pyproject.toml`` file to describe the project and how it9is built into a package. You might upload this package to PyPI, or distribute it10to your users in another way.1112Python installers create executable scripts that will run a specified Python13function. These are known as "entry points". The installer knows how to create14an executable regardless of the operating system, so it will work on Linux,15Windows, MacOS, etc.161718Project Files19-------------2021To install your app with an entry point, all you need is the script and a22``pyproject.toml`` file. Here's an example project directory:2324.. code-block:: text2526 hello-project/27 src/28 hello/29 __init__.py30 pyproject.toml3132Contents of ``hello.py``:3334.. click:example::3536 import click3738 @click.command()39 def cli():40 """Prints a greeting."""41 click.echo("Hello, World!")4243Contents of ``pyproject.toml``:4445.. code-block:: toml4647 [project]48 name = "hello"49 version = "1.0.0"50 description = "Hello CLI"51 requires-python = ">=3.11"52 dependencies = [53 "click>=8.1",54 ]5556 [project.scripts]57 hello = "hello:cli"5859 [build-system]60 requires = ["flit_core<4"]61 build-backend = "flit_core.buildapi"6263The magic is in the ``project.scripts`` section. Each line identifies one executable64script. The first part before the equals sign (``=``) is the name of the script that65should be generated, the second part is the import path followed by a colon66(``:``) with the function to call (the Click command).676869Installation70------------7172When your package is installed, the installer will create an executable script73based on the configuration. During development, you can install in editable74mode using the ``-e`` option. Remember to use a virtual environment!7576.. code-block:: console7778 $ python -m venv .venv79 $ . .venv/bin/activate80 $ pip install -e .8182Afterwards, your command should be available:8384.. click:run::8586 invoke(cli, prog_name="hello")