Skip to content
Myr

setuptools.rst

This file

Path

docs/setuptools.rst

155 lines · 4.4 KiB

Last changed by

Doc Update Resolving Issue #2206 (#2726)Adam Englanderbefore adoptione88c112

History of this file

Take it

1.. _setuptools-integration: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.  Read the full85`entry_points <https://packaging.python.org/en/latest/specifications/entry-points/>`_86specification for more details.  Below ``console_scripts``, each87line identifies one console script.  The first part before the88equals sign (``=``) is the name of the script that should be89generated, the second part is the import path followed by a colon90(``:``) with the Click command.9192That's it.9394Testing The Script95------------------9697To test the script, you can make a new virtualenv and then install your98package:99100.. code-block:: console101102    $ python3 -m venv .venv103    $ . .venv/bin/activate104    $ pip install --editable .105106Afterwards, your command should be available:107108.. click:run::109110    invoke(cli, prog_name='yourscript')111112Scripts in Packages113-------------------114115If your script is growing and you want to switch over to your script being116contained in a Python package the changes necessary are minimal.  Let's117assume your directory structure changed to this:118119.. code-block:: text120121    project/122        yourpackage/123            __init__.py124            main.py125            utils.py126            scripts/127                __init__.py128                yourscript.py129        setup.py130131In this case instead of using ``py_modules`` in your ``setup.py`` file you132can use ``packages`` and the automatic package finding support of133setuptools.  In addition to that it's also recommended to include other134package data.135136These would be the modified contents of ``setup.py``:137138.. code-block:: python139140    from setuptools import setup, find_packages141142    setup(143        name='yourpackage',144        version='0.1.0',145        packages=find_packages(),146        include_package_data=True,147        install_requires=[148            'Click',149        ],150        entry_points={151            'console_scripts': [152                'yourscript = yourpackage.scripts.yourscript:cli',153            ],154        },155    )