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
Provenance
- Commit
61b6ffa- Tree
02b2432- Author
Armin Ronacher <armin.ronacher@active-4.com>- Committer
Armin Ronacher <armin.ronacher@active-4.com>- Parent
a4d02ff
4 files changed+201 added−36 removed
modified:docs/clickdoctools.py+5 added-5 removed
128 unmodified lines129 def run(self, source):130 code = compile(source, '<docs>', 'exec')131 buffer = []132 133- 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 line139 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 line150 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 linesmodified:docs/index.rst+3 added-3 removed
36 unmodified lines37 And what it looks like when run:38 39 .. click:run::40 41- invoke(hello, ['--count=3'], input='John\n')41+ invoke(hello, ['--count=3'], prog_name='python hello.py', input='John\n')42 43 It automatically generates nicely formatted help pages:44 45 .. click:run::46 47- invoke(hello, ['--help'])47+ invoke(hello, ['--help'], prog_name='python hello.py')48 49 You can get the library directly from PyPI::50 51 pip install click8 unmodified lines60 :maxdepth: 261 62 why63 quickstart64+ setuptools65 parameters66 options67 arguments68 commands69 prompts70 documentation70- setuptools71 complex72 advanced73 testing74 utils24 unmodified linesmodified:docs/quickstart.rst+112 added-3 removed
5 unmodified lines6 You can get the library directly from PyPI::7 8 pip install click9 10+The installation into a :ref:`virtualenv` is heavily recommended.11+12+.. _virtualenv:13+14+virtualenv15+----------16+17+Virtualenv is probably what you want to use for developing Click18+applications.19+20+What problem does virtualenv solve? Chances are that you want to use it21+for other projects besides your Click script. But the more projects you22+have, the more likely it is that you will be working with different23+versions of Python itself, or at least different versions of Python24+libraries. Let's face it: quite often libraries break backwards25+compatibility, and it's unlikely that any serious application will have26+zero dependencies. So what do you do if two or more of your projects have27+conflicting dependencies?28+29+Virtualenv to the rescue! Virtualenv enables multiple side-by-side30+installations of Python, one for each project. It doesn't actually31+install separate copies of Python, but it does provide a clever way to32+keep different project environments isolated. Let's see how virtualenv33+works.34+35+If you are on Mac OS X or Linux, chances are that one of the following two36+commands will work for you::37+38+ $ sudo easy_install virtualenv39+40+or even better::41+42+ $ sudo pip install virtualenv43+44+One of these will probably install virtualenv on your system. Maybe it's even45+in your package manager. If you use Ubuntu, try::46+47+ $ sudo apt-get install python-virtualenv48+49+If you are on Windows (or none of the above methods worked) you must install50+``pip`` first. For more information about this, see `installing pip`_.51+Once you have it installed, run the ``pip`` command from above, but without52+the `sudo` prefix.53+54+.. _installing pip: http://pip.readthedocs.org/en/latest/installing.html55+56+Once you have virtualenv installed, just fire up a shell and create57+your own environment. I usually create a project folder and a `venv`58+folder within::59+60+ $ mkdir myproject61+ $ cd myproject62+ $ virtualenv venv63+ New python executable in venv/bin/python64+ Installing setuptools, pip............done.65+66+Now, whenever you want to work on a project, you only have to activate the67+corresponding environment. On OS X and Linux, do the following::68+69+ $ . venv/bin/activate70+71+If you are a Windows user, the following command is for you::72+73+ $ venv\scripts\activate74+75+Either way, you should now be using your virtualenv (notice how the prompt of76+your shell has changed to show the active environment).77+78+And if you want to go back to the real world, use the following command::79+80+ $ deactivate81+82+After doing this, the prompt of your shell should be as familar as before.83+84+Now, let's move on. Enter the following command to get Flask activated in your85+virtualenv::86+87+ $ pip install Click88+89+A few seconds later and you are good to go.90+91 Screencast and Examples92 -----------------------93 94 There is a screencast available which shows the basic API of Click and53 unmodified lines148 And what it looks like:149 150 .. click:run::151 71- invoke(hello, args=[])152+ invoke(hello, args=[], prog_name='python hello.py')153 154 And the corresponding help page:155 156 .. click:run::157 77- invoke(hello, args=['--help'])158+ invoke(hello, args=['--help'], prog_name='python hello.py')159 160 Echoing161 -------162 79 unmodified lines242 What it looks like:243 244 .. click:run::245 165- invoke(hello, args=['--help'])246+ invoke(hello, args=['--help'], prog_name='python hello.py')247+248+.. _switching-to-setuptools:249+250+Switching to Setuptools251+-----------------------252+253+In the code you wrote so far there is a block at the end of the file which254+looks like this: ``if __name__ == '__main__':``. This is traditionally255+how a standalone Python file looks like. With Click you can continue256+doing that, but there are better ways through setuptools.257+258+There are two main (and many more) reasons for this:259+260+The first one is that setuptools automatically generates executable261+wrappers for Windows so your command line utilities work on Windows too.262+263+The second reason is that setuptools scripts work with virtualenv on Unix264+without the virtualenv having to be activated. This is a very useful265+concept which allows you to bundle your scripts with all requirements into266+a virtualenv.267+268+Click is perfectly equipped to work with that and in fact the rest of the269+documentation will assume that you are writing applications through270+setuptools.271+272+I strongly recommend to have a look at the :ref:`setuptools-integration`273+chapter before reading the rest as the examples assume that you will274+be using setuptools.modified:docs/setuptools.rst+81 added-25 removed
1 .. _setuptools-integration:2 3-Usage with Setuptools4-=====================3+Setuptools Integration4+======================5 6 When writing command line utilities, it's recommended to write them as7 modules that are distributed with setuptools instead of using Unix8-shebangs. There are many reasons for this.8+shebangs.9 10-The first one is that setuptools automatically generates executable11-wrappers for Windows so your command line utilities work on Windows too.10+Why would you want to do that? There are a bunch of reasons:11 13-The second reason is that setuptools scripts work with virtualenv on Unix14-without the virtualenv having to be activated. This is a very useful15-concept which allows you to bundle your scripts with all requirements into16-a virtualenv.12+1. 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.15 16+ 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.21+22+2. 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.30+31+ 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.35+36+3. 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+40 Introduction41 ------------42 21-To bundle your script with setuptools, all you need is the script in a Python22-package and a ``setup.py`` file.43+To bundle your script with setuptools, all you need is the script in a44+Python package and a ``setup.py`` file.45 46 Imagine this directory structure::47 26- yourpackage/27- cli.py48+ yourscript.py49 setup.py50 30-Contents of ``cli.py``:51+Contents of ``yourscript.py``:52 53 .. click:example::54 55 import click4 unmodified lines60 click.echo('Hello World!')61 62 Contents of ``setup.py``::63 43- from setuptools import setup, find_packages64+ from setuptools import setup65 66 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 )78 59-The magic is in the ``entry_points`` parameter. Below ``console_scripts``,60-each line identifies one console script. The first part before the equals61-sign (``=``) is the name of the script that should be generated, the second62-part is the import path followed by a colon (``:``) with the Click63-command.79+The magic is in the ``entry_points`` parameter. Below80+``console_scripts``, each line identifies one console script. The first81+part before the equals sign (``=``) is the name of the script that should82+be generated, the second part is the import path followed by a colon83+(``:``) with the Click command.84 85 That's it.86 87 Testing The Script9 unmodified lines97 Afterwards, your command should be available:98 99 .. click:run::100 81- invoke(cli, prog_name='yourscript', prog_prefix='')101+ invoke(cli, prog_name='yourscript')102+103+Scripts in Packages104+-------------------105+106+If your script is growing and you want to switch over to your script being107+contained in a Python package the changes necessary are minimal. Let's108+assume your directory structure changed to this::109+110+ yourpackage/111+ main.py112+ utils.py113+ scripts/114+ yourscript.py115+116+In this case instead of using ``py_modules`` in your ``setup.py`` file you117+can use ``packages`` and the automatic package finding support of118+setuptools. In addition to that it's also recommended to include other119+package data.120+121+These would be the modified contents of ``setup.py``::122+123+ from setuptools import setup, find_packages124+125+ 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+ )