setuptools.rst
This file
Path
docs/setuptools.rst
81 lines · 2.0 KiB
Last changed by
Merge pull request #109 from iKevinY/doc-fixesArmin Ronacherbefore adoptiond428e69
History of this fileTake it
The walk stopped after 200 commits: lines older than 09d219d point at it.
Added basic bash complete support.Armin Ronacherbefore adoption4fa40cf
lines 1–2why, one step back1.. _setuptools-integration:2Added travis configArmin Ronacherbefore adoption09d219d
lines 3–21why, one step back3Usage with Setuptools4=====================56When writing command line utilities it's recommended to write them as7modules that are distributed with setuptools instead of using UNIX8shebangs. There are a bunch of reasons for this.910The first one is that setuptools automatically generates executable11wrappers for Windows so your command line utilities work on Windows too.1213The second reason is that setuptools scripts work with virtualenv on UNIX14without the virtualenv having to be activated. This is a very useful15concept which allows you to bundle your scripts with all requirements into16a virtualenv.1718Introduction19------------2021All you need to do to bundle your scripts with setuptools is your scriptMerge pull request #109 from iKevinY/doc-fixesArmin Ronacherbefore adoptiond428e69
line 22why, one step back22in a Python package and a ``setup.py`` file.Added travis configArmin Ronacherbefore adoption09d219d
line 23why, one step back23Merge pull request #109 from iKevinY/doc-fixesArmin Ronacherbefore adoptiond428e69
line 24why, one step back24Imagine this directory structure::Added travis configArmin Ronacherbefore adoption09d219d
lines 25–58why, one step back2526 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 )58Merge pull request #109 from iKevinY/doc-fixesArmin Ronacherbefore adoptiond428e69
line 59why, one step back59The magic is in the ``entry_points`` parameter. Below ``console_scripts``,Added travis configArmin Ronacherbefore adoption09d219d
lines 60–81why, one step back60each line identifies one console script. The first part before the equals61sign (``=``) 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------------------6970To test the script you can make a new virtualenv and then install your71package::7273 $ virtualenv venv74 $ . venv/bin/activate75 $ pip install --editable .7677Afterwards your command should be available:7879.. click:run::8081 invoke(cli, prog_name='yourscript', prog_prefix='')