switch to flit build backend (#2543)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by David Lord · committed by GitHub · parents 2de3b31, dbbdf42 · Browse files · 33 files·+308 added−334 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
33 files+308 added−334 removed
- other:+308 added−334 removed
Provenance
- Commit
3fcf338- Tree
a65d7c2- Author
David Lord <davidism@gmail.com>- Committer
GitHub <noreply@github.com>- Parent 1
2de3b31- Parent 2
dbbdf42
33 files changed+308 added−334 removed
modified:.github/workflows/tests.yaml-5 removed
39 unmodified lines404142434445464748444546474 unmodified lines39 unmodified lines with: python-version: ${{ matrix.python }} cache: 'pip' cache-dependency-path: 'requirements/*.txt' - name: update pip run: | pip install -U wheel pip install -U setuptools python -m pip install -U pip - name: cache mypy uses: actions/cache@88522ab9f39a2ea568f7027eddc7d8d8bc9d59c8 with: path: ./.mypy_cache4 unmodified linesmodified: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 linesadded:examples/aliases/pyproject.toml+18 added
123456789101112131415161718[project]name = "click-example-aliases"version = "1.0.0"description = "Click aliases example"requires-python = ">=3.7"dependencies = [ "click>=8.1",]
[project.scripts]aliases = "aliases:cli"
[build-system]requires = ["flit_core<4"]build-backend = "flit_core.buildapi"
[tool.flit.module]name = "aliases"deleted:examples/aliases/setup.py-13 removed
1-from setuptools import setup2-3-setup(4- name="click-example-aliases",5- version="1.0",6- py_modules=["aliases"],7- include_package_data=True,8- install_requires=["click"],9- entry_points="""10- [console_scripts]11- aliases=aliases:cli12- """,13-)added:examples/colors/pyproject.toml+18 added
1+[project]2+name = "click-example-colors"3+version = "1.0.0"4+description = "Click colors example"5+requires-python = ">=3.7"6+dependencies = [7+ "click>=8.1",8+]9+10+[project.scripts]11+colors = "colors:cli"12+13+[build-system]14+requires = ["flit_core<4"]15+build-backend = "flit_core.buildapi"16+17+[tool.flit.module]18+name = "colors"deleted:examples/colors/setup.py-13 removed
1-from setuptools import setup2-3-setup(4- name="click-example-colors",5- version="1.0",6- py_modules=["colors"],7- include_package_data=True,8- install_requires=["click"],9- entry_points="""10- [console_scripts]11- colors=colors:cli12- """,13-)added:examples/completion/pyproject.toml+18 added
1+[project]2+name = "click-example-completion"3+version = "1.0.0"4+description = "Click completion example"5+requires-python = ">=3.7"6+dependencies = [7+ "click>=8.1",8+]9+10+[project.scripts]11+completion = "completion:cli"12+13+[build-system]14+requires = ["flit_core<4"]15+build-backend = "flit_core.buildapi"16+17+[tool.flit.module]18+name = "completion"modified:examples/completion/README+5 added
24 unmodified lines25 26 eval (env _COMPLETION_COMPLETE=fish_source completion)27 28 Now press tab (maybe twice) after typing something to see completions.29+30+.. code-block:: python31+32+ $ completion <TAB> <TAB>33+ $ completion gr <TAB> <TAB>deleted:examples/completion/setup.py-13 removed
1-from setuptools import setup2-3-setup(4- name="click-example-completion",5- version="1.0",6- py_modules=["completion"],7- include_package_data=True,8- install_requires=["click"],9- entry_points="""10- [console_scripts]11- completion=completion:cli12- """,13-)added:examples/complex/pyproject.toml+18 added
1+[project]2+name = "click-example-complex"3+version = "1.0.0"4+description = "Click complex example"5+requires-python = ">=3.7"6+dependencies = [7+ "click>=8.1",8+]9+10+[project.scripts]11+complex = "complex.cli:cli"12+13+[build-system]14+requires = ["flit_core<4"]15+build-backend = "flit_core.buildapi"16+17+[tool.flit.module]18+name = "complex"deleted:examples/complex/setup.py-13 removed
1-from setuptools import setup2-3-setup(4- name="click-example-complex",5- version="1.0",6- packages=["complex", "complex.commands"],7- include_package_data=True,8- install_requires=["click"],9- entry_points="""10- [console_scripts]11- complex=complex.cli:cli12- """,13-)added:examples/imagepipe/pyproject.toml+19 added
1+[project]2+name = "click-example-imagepipe"3+version = "1.0.0"4+description = "Click imagepipe example"5+requires-python = ">=3.7"6+dependencies = [7+ "click>=8.1",8+ "pillow",9+]10+11+[project.scripts]12+imagepipe = "imagepipe:cli"13+14+[build-system]15+requires = ["flit_core<4"]16+build-backend = "flit_core.buildapi"17+18+[tool.flit.module]19+name = "imagepipe"deleted:examples/imagepipe/setup.py-13 removed
1-from setuptools import setup2-3-setup(4- name="click-example-imagepipe",5- version="1.0",6- py_modules=["imagepipe"],7- include_package_data=True,8- install_requires=["click", "pillow"],9- entry_points="""10- [console_scripts]11- imagepipe=imagepipe:cli12- """,13-)added:examples/inout/pyproject.toml+18 added
1+[project]2+name = "click-example-inout"3+version = "1.0.0"4+description = "Click inout example"5+requires-python = ">=3.7"6+dependencies = [7+ "click>=8.1",8+]9+10+[project.scripts]11+inout = "inout:cli"12+13+[build-system]14+requires = ["flit_core<4"]15+build-backend = "flit_core.buildapi"16+17+[tool.flit.module]18+name = "inout"deleted:examples/inout/setup.py-13 removed
1-from setuptools import setup2-3-setup(4- name="click-example-inout",5- version="0.1",6- py_modules=["inout"],7- include_package_data=True,8- install_requires=["click"],9- entry_points="""10- [console_scripts]11- inout=inout:cli12- """,13-)added:examples/naval/pyproject.toml+18 added
1+[project]2+name = "click-example-naval"3+version = "1.0.0"4+description = "Click naval example"5+requires-python = ">=3.7"6+dependencies = [7+ "click>=8.1",8+]9+10+[project.scripts]11+naval = "naval:cli"12+13+[build-system]14+requires = ["flit_core<4"]15+build-backend = "flit_core.buildapi"16+17+[tool.flit.module]18+name = "naval"deleted:examples/naval/setup.py-13 removed
1-from setuptools import setup2-3-setup(4- name="click-example-naval",5- version="2.0",6- py_modules=["naval"],7- include_package_data=True,8- install_requires=["click"],9- entry_points="""10- [console_scripts]11- naval=naval:cli12- """,13-)added:examples/repo/pyproject.toml+18 added
1+[project]2+name = "click-example-repo"3+version = "1.0.0"4+description = "Click repo example"5+requires-python = ">=3.7"6+dependencies = [7+ "click>=8.1",8+]9+10+[project.scripts]11+repo = "repo:cli"12+13+[build-system]14+requires = ["flit_core<4"]15+build-backend = "flit_core.buildapi"16+17+[tool.flit.module]18+name = "repo"deleted:examples/repo/setup.py-13 removed
1-from setuptools import setup2-3-setup(4- name="click-example-repo",5- version="0.1",6- py_modules=["repo"],7- include_package_data=True,8- install_requires=["click"],9- entry_points="""10- [console_scripts]11- repo=repo:cli12- """,13-)added:examples/termui/pyproject.toml+18 added
1+[project]2+name = "click-example-termui"3+version = "1.0.0"4+description = "Click termui example"5+requires-python = ">=3.7"6+dependencies = [7+ "click>=8.1",8+]9+10+[project.scripts]11+termui = "termui:cli"12+13+[build-system]14+requires = ["flit_core<4"]15+build-backend = "flit_core.buildapi"16+17+[tool.flit.module]18+name = "termui"deleted:examples/termui/setup.py-13 removed
1-from setuptools import setup2-3-setup(4- name="click-example-termui",5- version="1.0",6- py_modules=["termui"],7- include_package_data=True,8- install_requires=["click"],9- entry_points="""10- [console_scripts]11- termui=termui:cli12- """,13-)added:examples/validation/pyproject.toml+18 added
1+[project]2+name = "click-example-validation"3+version = "1.0.0"4+description = "Click validation example"5+requires-python = ">=3.7"6+dependencies = [7+ "click>=8.1",8+]9+10+[project.scripts]11+validation = "validation:cli"12+13+[build-system]14+requires = ["flit_core<4"]15+build-backend = "flit_core.buildapi"16+17+[tool.flit.module]18+name = "validation"deleted:examples/validation/setup.py-13 removed
1-from setuptools import setup2-3-setup(4- name="click-example-validation",5- version="1.0",6- py_modules=["validation"],7- include_package_data=True,8- install_requires=["click"],9- entry_points="""10- [console_scripts]11- validation=validation:cli12- """,13-)modified:examples/README-7 removed
1 unmodified line2 3 This folder contains various Click examples. Note that4 all of these are not runnable by themselves but should be5 installed into a virtualenv.6-7- This is done this way so that scripts also properly work8- on Windows and in virtualenvs without accidentally executing9- through the wrong interpreter.10-11- For more information about this see the documentation:12- https://click.palletsprojects.com/setuptools/modified:CHANGES.rst+1 added
5 unmodified lines6 Unreleased7 8 - Use modern packaging metadata with ``pyproject.toml`` instead of ``setup.cfg``.9 :pr:`326`10+- Use ``flit_core`` instead of ``setuptools`` as build backend.11 12 13 Version 8.1.414 -------------1052 unmodified linesmodified:CONTRIBUTING.rst-6 removed
105 unmodified lines106 .. code-block:: text107 108 > env\Scripts\activate109 110-- Upgrade pip and setuptools.111-112- .. code-block:: text113-114- $ python -m pip install --upgrade pip setuptools115-110 - Install the development dependencies, then install Click in111 editable mode.112 113 .. code-block:: text103 unmodified linesdeleted:MANIFEST.in-10 removed
1-include CHANGES.rst2-include tox.ini3-include requirements/*.txt4-graft artwork5-graft docs6-prune docs/_build7-graft examples8-graft tests9-include src/click/py.typed10-global-exclude *.pycmodified:pyproject.toml+18 added-5 removed
1 [project]2 name = "click"3 description = "Composable command line interface toolkit"4 readme = "README.rst"5-license = {text = "BSD-3-Clause"}5+license = {file = "LICENSE.rst"}6 maintainers = [{name = "Pallets", email = "contact@palletsprojects.com"}]7 classifiers = [8 "Development Status :: 5 - Production/Stable",9 "Intended Audience :: Developers",16 unmodified lines26 "Issue Tracker" = "https://github.com/pallets/click/issues/"27 Chat = "https://discord.gg/pallets"28 29 [build-system]30-requires = ["setuptools"]31-build-backend = "setuptools.build_meta"30+requires = ["flit_core<4"]31+build-backend = "flit_core.buildapi"32 33-[tool.setuptools.dynamic]34-version = {attr = "click.__version__"}33+[tool.flit.module]34+name = "click"35 36+[tool.flit.sdist]37+include = [38+ "docs/",39+ "examples/",40+ "requirements/",41+ "tests/",42+ "CHANGES.rst",43+ "tox.ini",44+]45+exclude = [46+ "docs/_build/",47+]48+49 [tool.pytest.ini_options]50 testpaths = ["tests"]51 filterwarnings = ["error"]52 22 unmodified lines