use pyproject.toml and flit in docs
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by David Lord · committed by David Lord · parent 1989ac2 · Browse files · 6 files·+103 added−171 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
6 files+103 added−171 removed
- other:+103 added−171 removed
Provenance
- Commit
34e6773- Tree
08989d4- Author
David Lord <davidism@gmail.com>- Committer
David Lord <davidism@gmail.com>- Parent
1989ac2
6 files changed+103 added−171 removed
modified:docs/commands.rst+3 added-5 removed
283 unmodified lines284 285 .. versionadded:: 3.0286 287 Sometimes it is useful to be allowed to invoke more than one subcommand in288-one go. For instance if you have installed a setuptools package before289-you might be familiar with the ``setup.py sdist bdist_wheel upload``290-command chain which invokes ``sdist`` before ``bdist_wheel`` before291-``upload``. Starting with Click 3.0 this is very simple to implement.292-All you have to do is to pass ``chain=True`` to your multicommand:288+one go. For example, ``my-app validate build upload`` would invoke ``validate``,289+then ``build``, then ``upload``. To implement this in Click, pass ``chain=True``290+when creating a group.291 292 .. click:example::293 294 @click.group(chain=True)275 unmodified linesadded:docs/entry-points.rst+86 added
1+Packaging Entry Points2+======================3+4+It's recommended to write command line utilities as installable packages with5+entry points instead telling users to run ``python hello.py``.6+7+A distribution package is a ``.whl`` file you install with pip or another Python8+installer. You use a ``pyproject.toml`` file to describe the project and how it9+is built into a package. You might upload this package to PyPI, or distribute it10+to your users in another way.11+12+Python installers create executable scripts that will run a specified Python13+function. These are known as "entry points". The installer knows how to create14+an executable regardless of the operating system, so it will work on Linux,15+Windows, MacOS, etc.16+17+18+Project Files19+-------------20+21+To install your app with an entry point, all you need is the script and a22+``pyproject.toml`` file. Here's an example project directory:23+24+.. code-block:: text25+26+ hello-project/27+ src/28+ hello/29+ __init__.py30+ pyproject.toml31+32+Contents of ``hello.py``:33+34+.. click:example::35+36+ import click37+38+ @click.command()39+ def cli():40+ """Prints a greeting."""41+ click.echo("Hello, World!")42+43+Contents of ``pyproject.toml``:44+45+.. code-block:: toml46+47+ [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+ ]55+56+ [project.scripts]57+ hello = "hello:cli"58+59+ [build-system]60+ requires = ["flit_core<4"]61+ build-backend = "flit_core.buildapi"62+63+The magic is in the ``project.scripts`` section. Each line identifies one executable64+script. The first part before the equals sign (``=``) is the name of the script that65+should be generated, the second part is the import path followed by a colon66+(``:``) with the function to call (the Click command).67+68+69+Installation70+------------71+72+When your package is installed, the installer will create an executable script73+based on the configuration. During development, you can install in editable74+mode using the ``-e`` option. Remember to use a virtual environment!75+76+.. code-block:: console77+78+ $ python -m venv .venv79+ $ . .venv/bin/activate80+ $ pip install -e .81+82+Afterwards, your command should be available:83+84+.. click:run::85+86+ invoke(cli, prog_name="hello")modified:docs/index.rst+1 added-1 removed
66 unmodified lines67 :maxdepth: 268 69 why70 quickstart71- setuptools71+ entry-points72 parameters73 options74 arguments75 commands32 unmodified linesmodified:docs/quickstart.rst+10 added-15 removed
54 unmodified lines55 $ mkdir myproject56 $ cd myproject57 $ virtualenv venv58 New python executable in venv/bin/python59- Installing setuptools, pip............done.59 60 Now, whenever you want to work on a project, you only have to activate the61 corresponding environment. On OS X and Linux, do the following::62 155 unmodified lines218 @cli.command()219 def dropdb():220 click.echo('Dropped the database')221 223-You would then invoke the :class:`Group` in your setuptools entry points or224-other invocations::222+You would then invoke the :class:`Group` in your entry points or other invocations::223 224 if __name__ == '__main__':225 cli()226 40 unmodified lines267 .. click:run::268 269 invoke(hello, args=['--help'], prog_name='python hello.py')270 273-.. _switching-to-setuptools:271+Switching to Entry Points272+-------------------------273 275-Switching to Setuptools276------------------------277-274 In the code you wrote so far there is a block at the end of the file which275 looks like this: ``if __name__ == '__main__':``. This is traditionally276 how a standalone Python file looks like. With Click you can continue281-doing that, but there are better ways through setuptools.277+doing that, but a better way is to package your app with an entry point.278 279 There are two main (and many more) reasons for this:280 285-The first one is that setuptools automatically generates executable281+The first one is that installers automatically generate executable282 wrappers for Windows so your command line utilities work on Windows too.283 288-The second reason is that setuptools scripts work with virtualenv on Unix284+The second reason is that entry point scripts work with virtualenv on Unix285 without the virtualenv having to be activated. This is a very useful286 concept which allows you to bundle your scripts with all requirements into287 a virtualenv.288 289 Click is perfectly equipped to work with that and in fact the rest of the294-documentation will assume that you are writing applications through295-setuptools.290+documentation will assume that you are writing applications as distributed291+packages.292 297-I strongly recommend to have a look at the :ref:`setuptools-integration`298-chapter before reading the rest as the examples assume that you will299-be using setuptools.293+Look at the :doc:`entry-points` chapter before reading the rest as the examples294+assume that you will be using entry points.modified:docs/setuptools.rst+2 added-149 removed
1-.. _setuptools-integration:1+:orphan:2 3 Setuptools Integration4 ======================5 6-When writing command line utilities, it's recommended to write them as7-modules that are distributed with setuptools instead of using Unix8-shebangs.9-10-Why would you want to do that? There are a bunch of reasons:11-12-1. One of the problems with the traditional approach is that the first13- module the Python interpreter loads has an incorrect name. This might14- sound like a small issue but it has quite significant implications.15-16- The first module is not called by its actual name, but the17- interpreter renames it to ``__main__``. While that is a perfectly18- valid name it means that if another piece of code wants to import from19- that module it will trigger the import a second time under its real20- name and all of a sudden your code is imported twice.21-22-2. Not on all platforms are things that easy to execute. On Linux and OS23- X you can add a comment to the beginning of the file (``#!/usr/bin/env24- python``) and your script works like an executable (assuming it has25- the executable bit set). This however does not work on Windows.26- While on Windows you can associate interpreters with file extensions27- (like having everything ending in ``.py`` execute through the Python28- interpreter) you will then run into issues if you want to use the29- script in a virtualenv.30-31- In fact running a script in a virtualenv is an issue with OS X and32- Linux as well. With the traditional approach you need to have the33- whole virtualenv activated so that the correct Python interpreter is34- used. Not very user friendly.35-36-3. The main trick only works if the script is a Python module. If your37- application grows too large and you want to start using a package you38- will run into issues.39-40-Introduction41-------------42-43-To bundle your script with setuptools, all you need is the script in a44-Python package and a ``setup.py`` file.45-46-Imagine this directory structure:47-48-.. code-block:: text49-50- yourscript.py51- setup.py52-53-Contents of ``yourscript.py``:54-55-.. click:example::56-57- import click58-59- @click.command()60- def cli():61- """Example script."""62- click.echo('Hello World!')63-64-Contents of ``setup.py``:65-66-.. code-block:: python67-68- from setuptools import setup69-70- setup(71- name='yourscript',72- version='0.1.0',73- py_modules=['yourscript'],74- install_requires=[75- 'Click',76- ],77- entry_points={78- 'console_scripts': [79- 'yourscript = yourscript:cli',80- ],81- },82- )83-84-The magic is in the ``entry_points`` parameter. Below85-``console_scripts``, each line identifies one console script. The first86-part before the equals sign (``=``) is the name of the script that should87-be generated, the second part is the import path followed by a colon88-(``:``) with the Click command.89-90-That's it.91-92-Testing The Script93-------------------94-95-To test the script, you can make a new virtualenv and then install your96-package:97-98-.. code-block:: console99-100- $ virtualenv venv101- $ . venv/bin/activate102- $ pip install --editable .103-104-Afterwards, your command should be available:105-106-.. click:run::107-108- invoke(cli, prog_name='yourscript')109-110-Scripts in Packages111--------------------112-113-If your script is growing and you want to switch over to your script being114-contained in a Python package the changes necessary are minimal. Let's115-assume your directory structure changed to this:116-117-.. code-block:: text118-119- project/120- yourpackage/121- __init__.py122- main.py123- utils.py124- scripts/125- __init__.py126- yourscript.py127- setup.py128-129-In this case instead of using ``py_modules`` in your ``setup.py`` file you130-can use ``packages`` and the automatic package finding support of131-setuptools. In addition to that it's also recommended to include other132-package data.133-134-These would be the modified contents of ``setup.py``:135-136-.. code-block:: python137-138- from setuptools import setup, find_packages139-140- setup(141- name='yourpackage',142- version='0.1.0',143- packages=find_packages(),144- include_package_data=True,145- install_requires=[146- 'Click',147- ],148- entry_points={149- 'console_scripts': [150- 'yourscript = yourpackage.scripts.yourscript:cli',151- ],152- },153- )6+Moved to :doc:`entry-points`.modified:docs/shell-completion.rst+1 added-1 removed
23 unmodified lines24 -------------------25 26 Completion is only available if a script is installed and invoked27 through an entry point, not through the ``python`` command. See28-:doc:`/setuptools`. Once the executable is installed, calling it with28+:doc:`entry-points`. Once the executable is installed, calling it with29 a special environment variable will put Click in completion mode.30 31 In order for completion to be used, the user needs to register a special32 function with their shell. The script is different for every shell, and255 unmodified lines