Skip to content
Myr

setuptools.rst

This file

Path

docs/setuptools.rst

76 lines · 1.9 KiB

Last changed by

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

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–56
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.py2728Contents of ``cli.py``::2930 import click3132 @click.command()33 def cli():34 """Example script."""35 print('Hello World!')3637Contents of ``setup.py``::3839 from setuptools import setup, find_packages4041 setup(42 name='yourpackage',43 version='0.1',44 packages=find_packages(),45 include_package_data=True,46 install_requires=[47 'Click',48 ],49 entry_points='''50 [console_scripts]51 yourscript=yourpackage.cli:cli52 ''',53 )5455The magic is in the ``entry_points`` parameter. Below ``console_scripts``56each line identifies one console script. The first part before the equals

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

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

Initial commitArmin Ronacherbefore adoption4101de3

lines 58–76
58part is the import path followed by a colon (``:``) with the click
59command.6061That's it.6263Testing The Script64------------------6566To test the script you can make a new virtualenv and then install your67package::6869 $ virtualenv venv70 $ . venv/bin/activate71 $ pip install --editable .7273Afterwards your command should be available::7475 $ yourscript76 Hello World!