setuptools.rst
This file
Path
docs/setuptools.rst
79 lines · 2.0 KiB
Last changed by
Implemented and exposed click.echoArmin Ronacherbefore adoption95c24b3
History of this fileTake it
1Usage with Setuptools2=====================34When writing command line utilities it's recommended to write them as5modules that are distributed with setuptools instead of using UNIX6shebangs. There are a bunch of reasons for this.78The first one is that setuptools automatically generates executableMerge pull request #11 from yelite/typo-fixArmin Ronacherbefore adoption9c8a3eb
line 9why, one step back9wrappers for Windows so your command line utilities work on Windows too.Initial commitArmin Ronacherbefore adoption4101de3
lines 10–271011The second reason is that setuptools scripts work with virtualenv on UNIX12without the virtualenv having to be activated. This is a very useful13concept which allows you to bundle your scripts with all requirements into14a virtualenv.1516Introduction17------------1819All you need to do to bundle your scripts with setuptools is your script20in a python package and a ``setup.py`` file.2122Imagine this layout::2324 yourpackage/25 cli.py26 setup.py27More automated docsArmin Ronacherbefore adoption8254b79
line 28why, one step back28Contents of ``cli.py``:29More automated docsArmin Ronacherbefore adoption8254b79
lines 30–31why, one step back30.. click:example::31Initial commitArmin Ronacherbefore adoption4101de3
lines 32–3632 import click3334 @click.command()35 def cli():36 """Example script."""Implemented and exposed click.echoArmin Ronacherbefore adoption95c24b3
line 37why, one step back37 click.echo('Hello World!')Initial commitArmin Ronacherbefore adoption4101de3
lines 38–583839Contents of ``setup.py``::4041 from setuptools import setup, find_packages4243 setup(44 name='yourpackage',45 version='0.1',46 packages=find_packages(),47 include_package_data=True,48 install_requires=[49 'Click',50 ],51 entry_points='''52 [console_scripts]53 yourscript=yourpackage.cli:cli54 ''',55 )5657The magic is in the ``entry_points`` parameter. Below ``console_scripts``58each line identifies one console script. The first part before the equalsMerge pull request #21 from julen/doc-fixesArmin Ronacherbefore adoptiona4b7d70
line 59why, one step back59sign (``=``) is the name of the script that should be generated, the secondInitial commitArmin Ronacherbefore adoption4101de3
lines 60–7460part is the import path followed by a colon (``:``) with the click61command.6263That's it.6465Testing The Script66------------------6768To test the script you can make a new virtualenv and then install your69package::7071 $ virtualenv venv72 $ . venv/bin/activate73 $ pip install --editable .74More automated docsArmin Ronacherbefore adoption8254b79
line 75why, one step back75Afterwards your command should be available:76More automated docsArmin Ronacherbefore adoption8254b79
lines 77–79why, one step back77.. click:run::7879 invoke(cli, prog_name='yourscript', prog_prefix='')