Skip to content
Myr

Rewrote the setuptools documentation.

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Armin Ronacher · committed by Armin Ronacher · parent a4d02ff · Browse files · 4 files+201 added−36 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

4 files+201 added−36 removed

  • other:+201 added−36 removed

4 files+201 added−36 removed

modified:docs/clickdoctools.py+5 added-5 removed
128 unmodified lines
129 def run(self, source):130 code = compile(source, '<docs>', 'exec')131 buffer = []132133 def invoke(cmd, args=None, prog_name=None, prog_prefix='python ',133 def invoke(cmd, args=None, prog_name=None,134 input=None, terminate_input=False, env=None,135 **extra):136 if env:137 for key, value in sorted(env.items()):
1 unmodified line
139 value = '"%s"' % value140 buffer.append('$ export %s=%s' % (key, value))141 args = args or []142 if prog_name is None:143 prog_name = cmd.name.replace('_', '-') + '.py'144 buffer.append(('$ %s%s %s' % (145 prog_prefix,143 prog_name = cmd.name.replace('_', '-')144 buffer.append(('$ %s %s' % (145 prog_name,146 ' '.join(('"%s"' % x) if ' ' in x else x for x in args)147 )).rstrip())148 if isinstance(input, (tuple, list)):
1 unmodified line
150 if terminate_input:151 input += '\xff'152 with isolation(input=input, env=env) as output:153 try:155 cmd.main(args=args, prog_name=prog_name, **extra)154 cmd.main(args=args, prog_name=prog_name.split()[-1],155 **extra)156 except SystemExit:157 pass158 buffer.extend(output.getvalue().splitlines())159
96 unmodified lines
modified:docs/index.rst+3 added-3 removed
36 unmodified lines
37And what it looks like when run:3839.. click:run::4041 invoke(hello, ['--count=3'], input='John\n')41 invoke(hello, ['--count=3'], prog_name='python hello.py', input='John\n')4243It automatically generates nicely formatted help pages:4445.. click:run::4647 invoke(hello, ['--help'])47 invoke(hello, ['--help'], prog_name='python hello.py')4849You can get the library directly from PyPI::5051 pip install click
8 unmodified lines
60 :maxdepth: 26162 why63 quickstart64 setuptools65 parameters66 options67 arguments68 commands69 prompts70 documentation70 setuptools71 complex72 advanced73 testing74 utils
24 unmodified lines
modified:docs/quickstart.rst+112 added-3 removed
5 unmodified lines
6You can get the library directly from PyPI::78 pip install click910The installation into a :ref:`virtualenv` is heavily recommended.1112.. _virtualenv:1314virtualenv15----------1617Virtualenv is probably what you want to use for developing Click18applications.1920What problem does virtualenv solve? Chances are that you want to use it21for other projects besides your Click script. But the more projects you22have, the more likely it is that you will be working with different23versions of Python itself, or at least different versions of Python24libraries. Let's face it: quite often libraries break backwards25compatibility, and it's unlikely that any serious application will have26zero dependencies. So what do you do if two or more of your projects have27conflicting dependencies?2829Virtualenv to the rescue! Virtualenv enables multiple side-by-side30installations of Python, one for each project. It doesn't actually31install separate copies of Python, but it does provide a clever way to32keep different project environments isolated. Let's see how virtualenv33works.3435If you are on Mac OS X or Linux, chances are that one of the following two36commands will work for you::3738 $ sudo easy_install virtualenv3940or even better::4142 $ sudo pip install virtualenv4344One of these will probably install virtualenv on your system. Maybe it's even45in your package manager. If you use Ubuntu, try::4647 $ sudo apt-get install python-virtualenv4849If you are on Windows (or none of the above methods worked) you must install50``pip`` first. For more information about this, see `installing pip`_.51Once you have it installed, run the ``pip`` command from above, but without52the `sudo` prefix.5354.. _installing pip: http://pip.readthedocs.org/en/latest/installing.html5556Once you have virtualenv installed, just fire up a shell and create57your own environment. I usually create a project folder and a `venv`58folder within::5960 $ mkdir myproject61 $ cd myproject62 $ virtualenv venv63 New python executable in venv/bin/python64 Installing setuptools, pip............done.6566Now, whenever you want to work on a project, you only have to activate the67corresponding environment. On OS X and Linux, do the following::6869 $ . venv/bin/activate7071If you are a Windows user, the following command is for you::7273 $ venv\scripts\activate7475Either way, you should now be using your virtualenv (notice how the prompt of76your shell has changed to show the active environment).7778And if you want to go back to the real world, use the following command::7980 $ deactivate8182After doing this, the prompt of your shell should be as familar as before.8384Now, let's move on. Enter the following command to get Flask activated in your85virtualenv::8687 $ pip install Click8889A few seconds later and you are good to go.9091Screencast and Examples92-----------------------9394There is a screencast available which shows the basic API of Click and
53 unmodified lines
148And what it looks like:149150.. click:run::15171 invoke(hello, args=[])152 invoke(hello, args=[], prog_name='python hello.py')153154And the corresponding help page:155156.. click:run::15777 invoke(hello, args=['--help'])158 invoke(hello, args=['--help'], prog_name='python hello.py')159160Echoing161-------162
79 unmodified lines
242What it looks like:243244.. click:run::245165 invoke(hello, args=['--help'])246 invoke(hello, args=['--help'], prog_name='python hello.py')247248.. _switching-to-setuptools:249250Switching to Setuptools251-----------------------252253In the code you wrote so far there is a block at the end of the file which254looks like this: ``if __name__ == '__main__':``. This is traditionally255how a standalone Python file looks like. With Click you can continue256doing that, but there are better ways through setuptools.257258There are two main (and many more) reasons for this:259260The first one is that setuptools automatically generates executable261wrappers for Windows so your command line utilities work on Windows too.262263The second reason is that setuptools scripts work with virtualenv on Unix264without the virtualenv having to be activated. This is a very useful265concept which allows you to bundle your scripts with all requirements into266a virtualenv.267268Click is perfectly equipped to work with that and in fact the rest of the269documentation will assume that you are writing applications through270setuptools.271272I strongly recommend to have a look at the :ref:`setuptools-integration`273chapter before reading the rest as the examples assume that you will274be using setuptools.
modified:docs/setuptools.rst+81 added-25 removed
1.. _setuptools-integration:23Usage with Setuptools4=====================3Setuptools Integration4======================56When writing command line utilities, it's recommended to write them as7modules that are distributed with setuptools instead of using Unix8shebangs. There are many reasons for this.8shebangs.910The first one is that setuptools automatically generates executable11wrappers for Windows so your command line utilities work on Windows too.10Why would you want to do that? There are a bunch of reasons:1113The 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.121. 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 it's 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 it's real20 name and all the 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------------4221To bundle your script with setuptools, all you need is the script in a Python22package and a ``setup.py`` file.43To bundle your script with setuptools, all you need is the script in a44Python package and a ``setup.py`` file.4546Imagine this directory structure::4726 yourpackage/27 cli.py48 yourscript.py49 setup.py5030Contents of ``cli.py``:51Contents of ``yourscript.py``:5253.. click:example::5455 import click
4 unmodified lines
60 click.echo('Hello World!')6162Contents of ``setup.py``::6343 from setuptools import setup, find_packages64 from setuptools import setup6566 setup(46 name='yourpackage',67 name='yourscript',68 version='0.1',48 packages=find_packages(),49 include_package_data=True,69 py_modules=['yourscript'],70 install_requires=[71 'Click',72 ],73 entry_points='''74 [console_scripts]55 yourscript=yourpackage.cli:cli75 yourscript=yourscript:cli76 ''',77 )7859The magic is in the ``entry_points`` parameter. Below ``console_scripts``,60each 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.79The magic is in the ``entry_points`` parameter. Below80``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.8485That's it.8687Testing The Script
9 unmodified lines
97Afterwards, your command should be available:9899.. click:run::10081 invoke(cli, prog_name='yourscript', prog_prefix='')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/111 main.py112 utils.py113 scripts/114 yourscript.py115116In this case instead of using ``py_modules`` in your ``setup.py`` file you117can use ``packages`` and the automatic package finding support of118setuptools. In addition to that it's also recommended to include other119package data.120121These would be the modified contents of ``setup.py``::122123 from setuptools import setup, find_packages124125 setup(126 name='yourpackage',127 version='0.1',128 packages=find_packages(),129 include_package_data=True,130 install_requires=[131 'Click',132 ],133 entry_points='''134 [console_scripts]135 yourscript=yourpackage.scripts.yourscript:cli136 ''',137 )