Skip to content
Myr

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 file

Take it

Initial commitArmin Ronacherbefore adoption4101de3

lines 1–8
1Usage with Setuptools
2=====================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 executable

Merge pull request #11 from yelite/typo-fixArmin Ronacherbefore adoption9c8a3eb

line 9why, one step back
9wrappers for Windows so your command line utilities work on Windows too.

Initial commitArmin Ronacherbefore adoption4101de3

lines 10–27
10
11The 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.py27

More automated docsArmin Ronacherbefore adoption8254b79

line 28why, one step back
28Contents of ``cli.py``:

Initial commitArmin Ronacherbefore adoption4101de3

line 29
29

More automated docsArmin Ronacherbefore adoption8254b79

lines 30–31why, one step back
30.. click:example::
31

Initial commitArmin Ronacherbefore adoption4101de3

lines 32–36
32 import click
3334 @click.command()35 def cli():36 """Example script."""

Implemented and exposed click.echoArmin Ronacherbefore adoption95c24b3

line 37why, one step back
37 click.echo('Hello World!')

Initial commitArmin Ronacherbefore adoption4101de3

lines 38–58
38
39Contents 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 equals

Merge pull request #21 from julen/doc-fixesArmin Ronacherbefore adoptiona4b7d70

line 59why, one step back
59sign (``=``) is the name of the script that should be generated, the second

Initial commitArmin Ronacherbefore adoption4101de3

lines 60–74
60part is the import path followed by a colon (``:``) with the click
61command.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 .74

More automated docsArmin Ronacherbefore adoption8254b79

line 75why, one step back
75Afterwards your command should be available:

Initial commitArmin Ronacherbefore adoption4101de3

line 76
76

More automated docsArmin Ronacherbefore adoption8254b79

lines 77–79why, one step back
77.. click:run::
7879 invoke(cli, prog_name='yourscript', prog_prefix='')