Skip to content
Myr

setuptools.rst

This file

Path

docs/setuptools.rst

139 lines · 4.1 KiB

Last changed by

Revert "use source command to activate virtualenv"Armin Ronacherbefore adoption5eca206

History of this file

Take it

The walk stopped after 200 commits: lines older than 5cae326 point at it.

Merge pull request #110 from krockode/patch-1Armin Ronacherbefore adoption5cae326

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

Rewrote the setuptools documentation.Armin Ronacherbefore adoption61b6ffa

lines 3–4why, one step back
3Setuptools Integration
4======================

Merge pull request #110 from krockode/patch-1Armin Ronacherbefore adoption5cae326

line 5why, one step back
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

Rewrote the setuptools documentation.Armin Ronacherbefore adoption61b6ffa

line 8why, one step back
8shebangs.

Merge pull request #110 from krockode/patch-1Armin Ronacherbefore adoption5cae326

line 9why, one step back
9

Rewrote the setuptools documentation.Armin Ronacherbefore adoption61b6ffa

line 10why, one step back
10Why would you want to do that? There are a bunch of reasons:

Merge pull request #110 from krockode/patch-1Armin Ronacherbefore adoption5cae326

line 11why, one step back
11

Rewrote the setuptools documentation.Armin Ronacherbefore adoption61b6ffa

lines 12–14why, one step back
121. One of the problems with the traditional approach is that the first
13 module the Python interpreter loads has an incorrect name. This might14 sound like a small issue but it has quite significant implications.

Merge pull request #110 from krockode/patch-1Armin Ronacherbefore adoption5cae326

line 15why, one step back
15

Merge pull request #333 from nchammas/patch-1Markus Unterwaditzerbefore adoption3d3bb75

line 16why, one step back
16 The first module is not called by its actual name, but the

Rewrote the setuptools documentation.Armin Ronacherbefore adoption61b6ffa

lines 17–18why, one step back
17 interpreter renames it to ``__main__``. While that is a perfectly
18 valid name it means that if another piece of code wants to import from

Merge pull request #333 from nchammas/patch-1Markus Unterwaditzerbefore adoption3d3bb75

lines 19–20why, one step back
19 that module it will trigger the import a second time under its real
20 name and all of a sudden your code is imported twice.

Rewrote the setuptools documentation.Armin Ronacherbefore adoption61b6ffa

lines 21–22why, one step back
21
222. Not on all platforms are things that easy to execute. On Linux and OS

Merge pull request #367 from lord63-forks/lord63-patch-1Markus Unterwaditzerbefore adoption218d8b5

line 23why, one step back
23 X you can add a comment to the beginning of the file (``#!/usr/bin/env

Rewrote the setuptools documentation.Armin Ronacherbefore adoption61b6ffa

lines 24–39why, one step back
24 python``) and your script works like an executable (assuming it has
25 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.39

Merge pull request #110 from krockode/patch-1Armin Ronacherbefore adoption5cae326

lines 40–42why, one step back
40Introduction
41------------42

Rewrote the setuptools documentation.Armin Ronacherbefore adoption61b6ffa

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

Merge pull request #110 from krockode/patch-1Armin Ronacherbefore adoption5cae326

lines 45–47why, one step back
45
46Imagine this directory structure::47

Rewrote the setuptools documentation.Armin Ronacherbefore adoption61b6ffa

line 48why, one step back
48 yourscript.py

Merge pull request #110 from krockode/patch-1Armin Ronacherbefore adoption5cae326

lines 49–50why, one step back
49 setup.py
50

Rewrote the setuptools documentation.Armin Ronacherbefore adoption61b6ffa

line 51why, one step back
51Contents of ``yourscript.py``:

Merge pull request #110 from krockode/patch-1Armin Ronacherbefore adoption5cae326

lines 52–63why, one step back
52
53.. click:example::5455 import click5657 @click.command()58 def cli():59 """Example script."""60 click.echo('Hello World!')6162Contents of ``setup.py``::63

Rewrote the setuptools documentation.Armin Ronacherbefore adoption61b6ffa

line 64why, one step back
64 from setuptools import setup

Merge pull request #110 from krockode/patch-1Armin Ronacherbefore adoption5cae326

lines 65–66why, one step back
65
66 setup(

Rewrote the setuptools documentation.Armin Ronacherbefore adoption61b6ffa

line 67why, one step back
67 name='yourscript',

Merge pull request #110 from krockode/patch-1Armin Ronacherbefore adoption5cae326

line 68why, one step back
68 version='0.1',

Rewrote the setuptools documentation.Armin Ronacherbefore adoption61b6ffa

line 69why, one step back
69 py_modules=['yourscript'],

Merge pull request #110 from krockode/patch-1Armin Ronacherbefore adoption5cae326

lines 70–74why, one step back
70 install_requires=[
71 'Click',72 ],73 entry_points='''74 [console_scripts]

Rewrote the setuptools documentation.Armin Ronacherbefore adoption61b6ffa

line 75why, one step back
75 yourscript=yourscript:cli

Merge pull request #110 from krockode/patch-1Armin Ronacherbefore adoption5cae326

lines 76–78why, one step back
76 ''',
77 )78

Rewrote the setuptools documentation.Armin Ronacherbefore adoption61b6ffa

lines 79–83why, one step back
79The magic is in the ``entry_points`` parameter. Below
80``console_scripts``, each line identifies one console script. The first81part before the equals sign (``=``) is the name of the script that should82be generated, the second part is the import path followed by a colon83(``:``) with the Click command.

Merge pull request #110 from krockode/patch-1Armin Ronacherbefore adoption5cae326

lines 84–89why, one step back
84
85That's it.8687Testing The Script88------------------89

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

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

Merge pull request #110 from krockode/patch-1Armin Ronacherbefore adoption5cae326

lines 91–93why, one step back
91package::
9293 $ virtualenv venv

Revert "use source command to activate virtualenv"Armin Ronacherbefore adoption5eca206

line 94why, one step back
94 $ . venv/bin/activate

Merge pull request #110 from krockode/patch-1Armin Ronacherbefore adoption5cae326

lines 95–96why, one step back
95 $ pip install --editable .
96

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

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

Merge pull request #110 from krockode/patch-1Armin Ronacherbefore adoption5cae326

lines 98–100why, one step back
98
99.. click:run::100

Rewrote the setuptools documentation.Armin Ronacherbefore adoption61b6ffa

lines 101–110why, one step back
101 invoke(cli, prog_name='yourscript')
102103Scripts in Packages104-------------------105106If your script is growing and you want to switch over to your script being107contained in a Python package the changes necessary are minimal. Let's108assume your directory structure changed to this::109110 yourpackage/

Merge pull request #242 from jaymartin7/patch-1Armin Ronacherbefore adoption438430d

line 111why, one step back
111 __init__.py

Rewrote the setuptools documentation.Armin Ronacherbefore adoption61b6ffa

lines 112–114why, one step back
112 main.py
113 utils.py114 scripts/

Merge pull request #242 from jaymartin7/patch-1Armin Ronacherbefore adoption438430d

line 115why, one step back
115 __init__.py

Rewrote the setuptools documentation.Armin Ronacherbefore adoption61b6ffa

lines 116–139why, one step back
116 yourscript.py
117118In this case instead of using ``py_modules`` in your ``setup.py`` file you119can use ``packages`` and the automatic package finding support of120setuptools. In addition to that it's also recommended to include other121package data.122123These would be the modified contents of ``setup.py``::124125 from setuptools import setup, find_packages126127 setup(128 name='yourpackage',129 version='0.1',130 packages=find_packages(),131 include_package_data=True,132 install_requires=[133 'Click',134 ],135 entry_points='''136 [console_scripts]137 yourscript=yourpackage.scripts.yourscript:cli138 ''',139 )