Skip to content
Myr

setuptools.rst

This file

Path

docs/setuptools.rst

81 lines · 2.0 KiB

Last changed by

Merge pull request #125 from iKevinY/doc-fixesArmin Ronacherbefore adoption1d25afd

History of this file

Take it

The walk stopped after 200 commits: lines older than 1726e47 point at it.

Added basic bash complete support.Armin Ronacherbefore adoption4fa40cf

lines 1–2why, one step back
1.. _setuptools-integration:
2

Exposed parserArmin Ronacherbefore adoption1726e47

lines 3–5why, one step back
3Usage with Setuptools
4=====================5

Documentation fixesKevin Yapbefore adoption67523af

line 6why, one step back
6When writing command line utilities, it's recommended to write them as

Merge pull request #113 from iKevinY/doc-fixesArmin Ronacherbefore adoptione0ea94a

line 7why, one step back
7modules that are distributed with setuptools instead of using Unix

Documentation fixesKevin Yapbefore adoption67523af

line 8why, one step back
8shebangs. There are many reasons for this.

Exposed parserArmin Ronacherbefore adoption1726e47

lines 9–12why, one step back
9
10The first one is that setuptools automatically generates executable11wrappers for Windows so your command line utilities work on Windows too.12

Merge pull request #113 from iKevinY/doc-fixesArmin Ronacherbefore adoptione0ea94a

line 13why, one step back
13The second reason is that setuptools scripts work with virtualenv on Unix

Exposed parserArmin Ronacherbefore adoption1726e47

lines 14–20why, one step back
14without the virtualenv having to be activated. This is a very useful
15concept which allows you to bundle your scripts with all requirements into16a virtualenv.1718Introduction19------------20

Documentation fixesKevin Yapbefore adoption67523af

lines 21–22why, one step back
21To bundle your script with setuptools, all you need is the script in a Python
22package and a ``setup.py`` file.

Exposed parserArmin Ronacherbefore adoption1726e47

line 23why, one step back
23

Merge pull request #109 from iKevinY/doc-fixesArmin Ronacherbefore adoptiond428e69

line 24why, one step back
24Imagine this directory structure::

Exposed parserArmin Ronacherbefore adoption1726e47

lines 25–58why, one step back
25
26 yourpackage/27 cli.py28 setup.py2930Contents of ``cli.py``:3132.. click:example::3334 import click3536 @click.command()37 def cli():38 """Example script."""39 click.echo('Hello World!')4041Contents of ``setup.py``::4243 from setuptools import setup, find_packages4445 setup(46 name='yourpackage',47 version='0.1',48 packages=find_packages(),49 include_package_data=True,50 install_requires=[51 'Click',52 ],53 entry_points='''54 [console_scripts]55 yourscript=yourpackage.cli:cli56 ''',57 )58

Merge pull request #109 from iKevinY/doc-fixesArmin Ronacherbefore adoptiond428e69

line 59why, one step back
59The magic is in the ``entry_points`` parameter. Below ``console_scripts``,

Exposed parserArmin Ronacherbefore adoption1726e47

lines 60–69why, one step back
60each line identifies one console script. The first part before the equals
61sign (``=``) is the name of the script that should be generated, the second62part is the import path followed by a colon (``:``) with the click63command.6465That's it.6667Testing The Script68------------------69

Merge pull request #125 from iKevinY/doc-fixesArmin Ronacherbefore adoption1d25afd

line 70why, one step back
70To test the script, you can make a new virtualenv and then install your

Exposed parserArmin Ronacherbefore adoption1726e47

lines 71–76why, one step back
71package::
7273 $ virtualenv venv74 $ . venv/bin/activate75 $ pip install --editable .76

Merge pull request #125 from iKevinY/doc-fixesArmin Ronacherbefore adoption1d25afd

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

Exposed parserArmin Ronacherbefore adoption1726e47

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