Skip to content
Myr

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

6 files+103 added−171 removed

modified:docs/commands.rst+3 added-5 removed
283 unmodified lines
284285.. versionadded:: 3.0286287Sometimes it is useful to be allowed to invoke more than one subcommand in288one go. For instance if you have installed a setuptools package before289you might be familiar with the ``setup.py sdist bdist_wheel upload``290command chain which invokes ``sdist`` before ``bdist_wheel`` before291``upload``. Starting with Click 3.0 this is very simple to implement.292All you have to do is to pass ``chain=True`` to your multicommand:288one go. For example, ``my-app validate build upload`` would invoke ``validate``,289then ``build``, then ``upload``. To implement this in Click, pass ``chain=True``290when creating a group.291292.. click:example::293294 @click.group(chain=True)
275 unmodified lines
added:docs/entry-points.rst+86 added
1Packaging 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")
modified:docs/index.rst+1 added-1 removed
66 unmodified lines
67 :maxdepth: 26869 why70 quickstart71 setuptools71 entry-points72 parameters73 options74 arguments75 commands
32 unmodified lines
modified:docs/quickstart.rst+10 added-15 removed
54 unmodified lines
55 $ mkdir myproject56 $ cd myproject57 $ virtualenv venv58 New python executable in venv/bin/python59 Installing setuptools, pip............done.5960Now, whenever you want to work on a project, you only have to activate the61corresponding environment. On OS X and Linux, do the following::62
155 unmodified lines
218 @cli.command()219 def dropdb():220 click.echo('Dropped the database')221223You would then invoke the :class:`Group` in your setuptools entry points or224other invocations::222You would then invoke the :class:`Group` in your entry points or other invocations::223224 if __name__ == '__main__':225 cli()226
40 unmodified lines
267.. click:run::268269 invoke(hello, args=['--help'], prog_name='python hello.py')270273.. _switching-to-setuptools:271Switching to Entry Points272-------------------------273275Switching to Setuptools276-----------------------277274In the code you wrote so far there is a block at the end of the file which275looks like this: ``if __name__ == '__main__':``. This is traditionally276how a standalone Python file looks like. With Click you can continue281doing that, but there are better ways through setuptools.277doing that, but a better way is to package your app with an entry point.278279There are two main (and many more) reasons for this:280285The first one is that setuptools automatically generates executable281The first one is that installers automatically generate executable282wrappers for Windows so your command line utilities work on Windows too.283288The second reason is that setuptools scripts work with virtualenv on Unix284The second reason is that entry point scripts work with virtualenv on Unix285without the virtualenv having to be activated. This is a very useful286concept which allows you to bundle your scripts with all requirements into287a virtualenv.288289Click is perfectly equipped to work with that and in fact the rest of the294documentation will assume that you are writing applications through295setuptools.290documentation will assume that you are writing applications as distributed291packages.292297I strongly recommend to have a look at the :ref:`setuptools-integration`298chapter before reading the rest as the examples assume that you will299be using setuptools.293Look at the :doc:`entry-points` chapter before reading the rest as the examples294assume that you will be using entry points.
modified:docs/setuptools.rst+2 added-149 removed
1.. _setuptools-integration:1:orphan:23Setuptools Integration4======================56When writing command line utilities, it's recommended to write them as7modules that are distributed with setuptools instead of using Unix8shebangs.910Why would you want to do that? There are a bunch of reasons:11121. 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.1516 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.21222. 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.3031 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.35363. 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.3940Introduction41------------4243To bundle your script with setuptools, all you need is the script in a44Python package and a ``setup.py`` file.4546Imagine this directory structure:4748.. code-block:: text4950 yourscript.py51 setup.py5253Contents of ``yourscript.py``:5455.. click:example::5657 import click5859 @click.command()60 def cli():61 """Example script."""62 click.echo('Hello World!')6364Contents of ``setup.py``:6566.. code-block:: python6768 from setuptools import setup6970 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 )8384The magic is in the ``entry_points`` parameter. Below85``console_scripts``, each line identifies one console script. The first86part before the equals sign (``=``) is the name of the script that should87be generated, the second part is the import path followed by a colon88(``:``) with the Click command.8990That's it.9192Testing The Script93------------------9495To test the script, you can make a new virtualenv and then install your96package:9798.. code-block:: console99100 $ virtualenv venv101 $ . venv/bin/activate102 $ pip install --editable .103104Afterwards, your command should be available:105106.. click:run::107108 invoke(cli, prog_name='yourscript')109110Scripts in Packages111-------------------112113If your script is growing and you want to switch over to your script being114contained in a Python package the changes necessary are minimal. Let's115assume your directory structure changed to this:116117.. code-block:: text118119 project/120 yourpackage/121 __init__.py122 main.py123 utils.py124 scripts/125 __init__.py126 yourscript.py127 setup.py128129In this case instead of using ``py_modules`` in your ``setup.py`` file you130can use ``packages`` and the automatic package finding support of131setuptools. In addition to that it's also recommended to include other132package data.133134These would be the modified contents of ``setup.py``:135136.. code-block:: python137138 from setuptools import setup, find_packages139140 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 )6Moved to :doc:`entry-points`.
modified:docs/shell-completion.rst+1 added-1 removed
23 unmodified lines
24-------------------2526Completion is only available if a script is installed and invoked27through 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 with29a special environment variable will put Click in completion mode.3031In order for completion to be used, the user needs to register a special32function with their shell. The script is different for every shell, and
255 unmodified lines