Skip to content
Myr

entry-points.rst

This file

Path

docs/entry-points.rst

87 lines · 2.3 KiB

Last changed by

Fix import path (#2941)Rowlando13before adoptionf9c1b88

History of this file

Take it

switch to flit build backend (#2543)David Lordbefore adoption3fcf338

lines 1–29why, one step back
1Packaging Entry Points
2======================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__.py

Fix entrypoints docs (#2923)Rowlando13before adoption1725157

line 30why, one step back
30 hello.py

switch to flit build backend (#2543)David Lordbefore adoption3fcf338

lines 31–57why, one step back
31 pyproject.toml
3233Contents of ``hello.py``:3435.. click:example::3637 import click3839 @click.command()40 def cli():41 """Prints a greeting."""42 click.echo("Hello, World!")4344Contents of ``pyproject.toml``:4546.. code-block:: toml4748 [project]49 name = "hello"50 version = "1.0.0"51 description = "Hello CLI"52 requires-python = ">=3.11"53 dependencies = [54 "click>=8.1",55 ]5657 [project.scripts]

Fix import path (#2941)Rowlando13before adoptionf9c1b88

line 58why, one step back
58 hello = "hello.hello:cli"

switch to flit build backend (#2543)David Lordbefore adoption3fcf338

lines 59–87why, one step back
59
60 [build-system]61 requires = ["flit_core<4"]62 build-backend = "flit_core.buildapi"6364The magic is in the ``project.scripts`` section. Each line identifies one executable65script. The first part before the equals sign (``=``) is the name of the script that66should be generated, the second part is the import path followed by a colon67(``:``) with the function to call (the Click command).686970Installation71------------7273When 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.. code-block:: console7879 $ python -m venv .venv80 $ . .venv/bin/activate81 $ pip install -e .8283Afterwards, your command should be available:8485.. click:run::8687 invoke(cli, prog_name="hello")