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 fileTake it
switch format from rest to mystdenuowebbefore adoption8677596
line 1why, one step back1# Packaging Entry Pointsrename extension from .rst to .mddenuowebbefore adoptiond1d3538
line 2why, one step back2switch format from rest to mystdenuowebbefore adoption8677596
lines 3–6why, one step back3```{eval-rst}4.. currentmodule:: click5```6rename extension from .rst to .mddenuowebbefore adoptiond1d3538
lines 7–19why, one step back7It'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.19switch format from rest to mystdenuowebbefore adoption8677596
line 20why, one step back20## Project Filesrename extension from .rst to .mddenuowebbefore adoptiond1d3538
lines 21–24why, one step back2122To install your app with an entry point, all you need is the script and a23``pyproject.toml`` file. Here's an example project directory:24switch format from rest to mystdenuowebbefore adoption8677596
lines 25–32why, one step back25```text26hello-project/27 src/28 hello/29 __init__.py30 hello.py31 pyproject.toml32```rename extension from .rst to .mddenuowebbefore adoptiond1d3538
lines 33–35why, one step back3334Contents of ``hello.py``:35switch format from rest to mystdenuowebbefore adoption8677596
line 36why, one step back36```{eval-rst}rename extension from .rst to .mddenuowebbefore adoptiond1d3538
lines 37–43why, one step back37.. 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 back44```rename extension from .rst to .mddenuowebbefore adoptiond1d3538
lines 45–47why, one step back4546Contents of ``pyproject.toml``:47switch format from rest to mystdenuowebbefore adoption8677596
lines 48–56why, one step back48```toml49[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 back57switch format from rest to mystdenuowebbefore adoption8677596
lines 58–59why, one step back58[project.scripts]59hello = "hello.hello:cli"rename extension from .rst to .mddenuowebbefore adoptiond1d3538
line 60why, one step back60switch format from rest to mystdenuowebbefore adoption8677596
lines 61–64why, one step back61[build-system]62requires = ["flit_core<4"]63build-backend = "flit_core.buildapi"64```rename extension from .rst to .mddenuowebbefore adoptiond1d3538
lines 65–70why, one step back6566The 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).70switch format from rest to mystdenuowebbefore adoption8677596
line 71why, one step back71## Installationrename extension from .rst to .mddenuowebbefore adoptiond1d3538
lines 72–76why, one step back7273When 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!76switch format from rest to mystdenuowebbefore adoption8677596
lines 77–81why, one step back77```console78$ python -m venv .venv79$ . .venv/bin/activate80$ pip install -e .81```rename extension from .rst to .mddenuowebbefore adoptiond1d3538
lines 82–84why, one step back8283Afterwards, your command should be available:84switch format from rest to mystdenuowebbefore adoption8677596
lines 85–88why, one step back85```console86$ hello87Hello, World!88```