Skip to content
Myr

setuptools.rst

This file

Path

docs/setuptools.rst

153 lines · 4.3 KiB

Last changed by

Merge pull request #1243 from bittner/masterDavid Lordbefore adoptionc6042bf

History of this file

Take it

The walk stopped after 200 commits: lines older than 752ff79 point at it.

Merge pull request #842 from axu2/masterMarkus Unterwaditzerbefore adoption752ff79

lines 1–45why, one step back
1.. _setuptools-integration:
23Setuptools Integration4======================56When writing command line utilities, it's recommended to write them as7modules that are distributed with setuptools instead of using Unix8shebangs.910Why would you want to do that? There are a bunch of reasons:11121. One of the problems with the traditional approach is that the first13 module the Python interpreter loads has an incorrect name. This might14 sound like a small issue but it has quite significant implications.1516 The first module is not called by its actual name, but the17 interpreter renames it to ``__main__``. While that is a perfectly18 valid name it means that if another piece of code wants to import from19 that module it will trigger the import a second time under its real20 name and all of a sudden your code is imported twice.21222. Not on all platforms are things that easy to execute. On Linux and OS23 X you can add a comment to the beginning of the file (``#!/usr/bin/env24 python``) and your script works like an executable (assuming it has25 the executable bit set). This however does not work on Windows.26 While on Windows you can associate interpreters with file extensions27 (like having everything ending in ``.py`` execute through the Python28 interpreter) you will then run into issues if you want to use the29 script in a virtualenv.3031 In fact running a script in a virtualenv is an issue with OS X and32 Linux as well. With the traditional approach you need to have the33 whole virtualenv activated so that the correct Python interpreter is34 used. Not very user friendly.35363. The main trick only works if the script is a Python module. If your37 application grows too large and you want to start using a package you38 will run into issues.3940Introduction41------------4243To bundle your script with setuptools, all you need is the script in a44Python package and a ``setup.py`` file.45

Merge pull request #1243 from bittner/masterDavid Lordbefore adoptionc6042bf

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

Merge pull request #842 from axu2/masterMarkus Unterwaditzerbefore adoption752ff79

line 47why, one step back
47

Merge pull request #1242 from bittner/docs/setuptools-markup-snippetsDavid Lordbefore adoption260f50d

lines 48–49why, one step back
48.. code-block:: text
49

Merge pull request #842 from axu2/masterMarkus Unterwaditzerbefore adoption752ff79

lines 50–63why, one step back
50 yourscript.py
51 setup.py5253Contents of ``yourscript.py``:5455.. click:example::5657 import click5859 @click.command()60 def cli():61 """Example script."""62 click.echo('Hello World!')63

Merge pull request #1242 from bittner/docs/setuptools-markup-snippetsDavid Lordbefore adoption260f50d

line 64why, one step back
64Contents of ``setup.py``:

Merge pull request #842 from axu2/masterMarkus Unterwaditzerbefore adoption752ff79

line 65why, one step back
65

Merge pull request #1242 from bittner/docs/setuptools-markup-snippetsDavid Lordbefore adoption260f50d

lines 66–67why, one step back
66.. code-block:: python
67

Merge pull request #842 from axu2/masterMarkus Unterwaditzerbefore adoption752ff79

lines 68–71why, one step back
68 from setuptools import setup
6970 setup(71 name='yourscript',

Merge pull request #1242 from bittner/docs/setuptools-markup-snippetsDavid Lordbefore adoption260f50d

line 72why, one step back
72 version='0.1.0',

Merge pull request #842 from axu2/masterMarkus Unterwaditzerbefore adoption752ff79

lines 73–76why, one step back
73 py_modules=['yourscript'],
74 install_requires=[75 'Click',76 ],

Merge pull request #1242 from bittner/docs/setuptools-markup-snippetsDavid Lordbefore adoption260f50d

lines 77–81why, one step back
77 entry_points={
78 'console_scripts': [79 'yourscript = yourscript:cli',80 ],81 },

Merge pull request #842 from axu2/masterMarkus Unterwaditzerbefore adoption752ff79

lines 82–95why, one step back
82 )
8384The magic is in the ``entry_points`` parameter. Below85``console_scripts``, each line identifies one console script. The first86part before the equals sign (``=``) is the name of the script that should87be generated, the second part is the import path followed by a colon88(``:``) with the Click command.8990That's it.9192Testing The Script93------------------9495To test the script, you can make a new virtualenv and then install your

Merge pull request #1242 from bittner/docs/setuptools-markup-snippetsDavid Lordbefore adoption260f50d

line 96why, one step back
96package:

Merge pull request #842 from axu2/masterMarkus Unterwaditzerbefore adoption752ff79

line 97why, one step back
97

Merge pull request #1242 from bittner/docs/setuptools-markup-snippetsDavid Lordbefore adoption260f50d

lines 98–99why, one step back
98.. code-block:: console
99

Merge pull request #842 from axu2/masterMarkus Unterwaditzerbefore adoption752ff79

lines 100–114why, one step back
100 $ virtualenv venv
101 $ . venv/bin/activate102 $ pip install --editable .103104Afterwards, your command should be available:105106.. click:run::107108 invoke(cli, prog_name='yourscript')109110Scripts in Packages111-------------------112113If your script is growing and you want to switch over to your script being114contained in a Python package the changes necessary are minimal. Let's

Merge pull request #1242 from bittner/docs/setuptools-markup-snippetsDavid Lordbefore adoption260f50d

line 115why, one step back
115assume your directory structure changed to this:

Merge pull request #842 from axu2/masterMarkus Unterwaditzerbefore adoption752ff79

line 116why, one step back
116

Merge pull request #1242 from bittner/docs/setuptools-markup-snippetsDavid Lordbefore adoption260f50d

lines 117–120why, one step back
117.. code-block:: text
118119 project/120 yourpackage/

Merge pull request #842 from axu2/masterMarkus Unterwaditzerbefore adoption752ff79

line 121why, one step back
121 __init__.py

Merge pull request #1242 from bittner/docs/setuptools-markup-snippetsDavid Lordbefore adoption260f50d

lines 122–127why, one step back
122 main.py
123 utils.py124 scripts/125 __init__.py126 yourscript.py127 setup.py

Merge pull request #842 from axu2/masterMarkus Unterwaditzerbefore adoption752ff79

lines 128–133why, one step back
128
129In this case instead of using ``py_modules`` in your ``setup.py`` file you130can use ``packages`` and the automatic package finding support of131setuptools. In addition to that it's also recommended to include other132package data.133

Merge pull request #1242 from bittner/docs/setuptools-markup-snippetsDavid Lordbefore adoption260f50d

lines 134–136why, one step back
134These would be the modified contents of ``setup.py``:
135136.. code-block:: python

Merge pull request #842 from axu2/masterMarkus Unterwaditzerbefore adoption752ff79

lines 137–141why, one step back
137
138 from setuptools import setup, find_packages139140 setup(141 name='yourpackage',

Use dict instead of multiline string (#1241)Peter Bittnerbefore adoptionf4d6ed8

line 142why, one step back
142 version='0.1.0',

Merge pull request #842 from axu2/masterMarkus Unterwaditzerbefore adoption752ff79

lines 143–147why, one step back
143 packages=find_packages(),
144 include_package_data=True,145 install_requires=[146 'Click',147 ],

Use dict instead of multiline string (#1241)Peter Bittnerbefore adoptionf4d6ed8

lines 148–152why, one step back
148 entry_points={
149 'console_scripts': [150 'yourscript = yourpackage.scripts.yourscript:cli',151 ],152 },

Merge pull request #842 from axu2/masterMarkus Unterwaditzerbefore adoption752ff79

line 153why, one step back
153 )