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. 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 )