Skip to content
Myr

setuptools.rst

This file

Path

docs/setuptools.rst

139 lines · 4.1 KiB

Last changed by

Not known.

History of this file

Take it

The walk stopped after 200 commits: lines older than 430c50a point at it.

Merge pull request #681 from pgkelley4/masterMarkus Unterwaditzerbefore adoption430c50a

lines 1–139why, one step back
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 yourscript.py49 setup.py5051Contents of ``yourscript.py``:5253.. click:example::5455 import click5657 @click.command()58 def cli():59 """Example script."""60 click.echo('Hello World!')6162Contents of ``setup.py``::6364 from setuptools import setup6566 setup(67 name='yourscript',68 version='0.1',69 py_modules=['yourscript'],70 install_requires=[71 'Click',72 ],73 entry_points='''74 [console_scripts]75 yourscript=yourscript:cli76 ''',77 )7879The magic is in the ``entry_points`` parameter. Below80``console_scripts``, each line identifies one console script. The first81part before the equals sign (``=``) is the name of the script that should82be generated, the second part is the import path followed by a colon83(``:``) with the Click command.8485That's it.8687Testing The Script88------------------8990To test the script, you can make a new virtualenv and then install your91package::9293 $ virtualenv venv94 $ . venv/bin/activate95 $ pip install --editable .9697Afterwards, your command should be available:9899.. click:run::100101 invoke(cli, prog_name='yourscript')102103Scripts in Packages104-------------------105106If your script is growing and you want to switch over to your script being107contained in a Python package the changes necessary are minimal. Let's108assume your directory structure changed to this::109110 yourpackage/111 __init__.py112 main.py113 utils.py114 scripts/115 __init__.py116 yourscript.py117118In this case instead of using ``py_modules`` in your ``setup.py`` file you119can use ``packages`` and the automatic package finding support of120setuptools. In addition to that it's also recommended to include other121package data.122123These would be the modified contents of ``setup.py``::124125 from setuptools import setup, find_packages126127 setup(128 name='yourpackage',129 version='0.1',130 packages=find_packages(),131 include_package_data=True,132 install_requires=[133 'Click',134 ],135 entry_points='''136 [console_scripts]137 yourscript=yourpackage.scripts.yourscript:cli138 ''',139 )