Éric Araujo | 3a9f58f | 2011-06-01 20:42:49 +0200 | [diff] [blame] | 1 | .. _packaging-examples: |
| 2 | |
| 3 | ******** |
| 4 | Examples |
| 5 | ******** |
| 6 | |
| 7 | This chapter provides a number of basic examples to help get started with |
| 8 | Packaging. |
| 9 | |
| 10 | |
| 11 | .. _packaging-pure-mod: |
| 12 | |
| 13 | Pure Python distribution (by module) |
| 14 | ==================================== |
| 15 | |
| 16 | If you're just distributing a couple of modules, especially if they don't live |
| 17 | in a particular package, you can specify them individually using the |
| 18 | :option:`py_modules` option in the setup script. |
| 19 | |
| 20 | In the simplest case, you'll have two files to worry about: a setup script and |
| 21 | the single module you're distributing, :file:`foo.py` in this example:: |
| 22 | |
| 23 | <root>/ |
| 24 | setup.py |
| 25 | foo.py |
| 26 | |
| 27 | (In all diagrams in this section, *<root>* will refer to the distribution root |
| 28 | directory.) A minimal setup script to describe this situation would be:: |
| 29 | |
| 30 | from packaging.core import setup |
| 31 | setup(name='foo', |
| 32 | version='1.0', |
| 33 | py_modules=['foo']) |
| 34 | |
| 35 | Note that the name of the distribution is specified independently with the |
| 36 | :option:`name` option, and there's no rule that says it has to be the same as |
| 37 | the name of the sole module in the distribution (although that's probably a good |
| 38 | convention to follow). However, the distribution name is used to generate |
| 39 | filenames, so you should stick to letters, digits, underscores, and hyphens. |
| 40 | |
| 41 | Since :option:`py_modules` is a list, you can of course specify multiple |
| 42 | modules, e.g. if you're distributing modules :mod:`foo` and :mod:`bar`, your |
| 43 | setup might look like this:: |
| 44 | |
| 45 | <root>/ |
| 46 | setup.py |
| 47 | foo.py |
| 48 | bar.py |
| 49 | |
| 50 | and the setup script might be :: |
| 51 | |
| 52 | from packaging.core import setup |
| 53 | setup(name='foobar', |
| 54 | version='1.0', |
| 55 | py_modules=['foo', 'bar']) |
| 56 | |
| 57 | You can put module source files into another directory, but if you have enough |
| 58 | modules to do that, it's probably easier to specify modules by package rather |
| 59 | than listing them individually. |
| 60 | |
| 61 | |
| 62 | .. _packaging-pure-pkg: |
| 63 | |
| 64 | Pure Python distribution (by package) |
| 65 | ===================================== |
| 66 | |
| 67 | If you have more than a couple of modules to distribute, especially if they are |
| 68 | in multiple packages, it's probably easier to specify whole packages rather than |
| 69 | individual modules. This works even if your modules are not in a package; you |
| 70 | can just tell the Distutils to process modules from the root package, and that |
| 71 | works the same as any other package (except that you don't have to have an |
| 72 | :file:`__init__.py` file). |
| 73 | |
| 74 | The setup script from the last example could also be written as :: |
| 75 | |
| 76 | from packaging.core import setup |
| 77 | setup(name='foobar', |
| 78 | version='1.0', |
| 79 | packages=['']) |
| 80 | |
| 81 | (The empty string stands for the root package.) |
| 82 | |
| 83 | If those two files are moved into a subdirectory, but remain in the root |
| 84 | package, e.g.:: |
| 85 | |
| 86 | <root>/ |
| 87 | setup.py |
| 88 | src/ |
| 89 | foo.py |
| 90 | bar.py |
| 91 | |
| 92 | then you would still specify the root package, but you have to tell the |
| 93 | Distutils where source files in the root package live:: |
| 94 | |
| 95 | from packaging.core import setup |
| 96 | setup(name='foobar', |
| 97 | version='1.0', |
| 98 | package_dir={'': 'src'}, |
| 99 | packages=['']) |
| 100 | |
| 101 | More typically, though, you will want to distribute multiple modules in the same |
| 102 | package (or in sub-packages). For example, if the :mod:`foo` and :mod:`bar` |
| 103 | modules belong in package :mod:`foobar`, one way to lay out your source tree is |
| 104 | |
| 105 | :: |
| 106 | |
| 107 | <root>/ |
| 108 | setup.py |
| 109 | foobar/ |
| 110 | __init__.py |
| 111 | foo.py |
| 112 | bar.py |
| 113 | |
| 114 | This is in fact the default layout expected by the Distutils, and the one that |
| 115 | requires the least work to describe in your setup script:: |
| 116 | |
| 117 | from packaging.core import setup |
| 118 | setup(name='foobar', |
| 119 | version='1.0', |
| 120 | packages=['foobar']) |
| 121 | |
| 122 | If you want to put modules in directories not named for their package, then you |
| 123 | need to use the :option:`package_dir` option again. For example, if the |
| 124 | :file:`src` directory holds modules in the :mod:`foobar` package:: |
| 125 | |
| 126 | <root>/ |
| 127 | setup.py |
| 128 | src/ |
| 129 | __init__.py |
| 130 | foo.py |
| 131 | bar.py |
| 132 | |
| 133 | an appropriate setup script would be :: |
| 134 | |
| 135 | from packaging.core import setup |
| 136 | setup(name='foobar', |
| 137 | version='1.0', |
| 138 | package_dir={'foobar': 'src'}, |
| 139 | packages=['foobar']) |
| 140 | |
| 141 | Or, you might put modules from your main package right in the distribution |
| 142 | root:: |
| 143 | |
| 144 | <root>/ |
| 145 | setup.py |
| 146 | __init__.py |
| 147 | foo.py |
| 148 | bar.py |
| 149 | |
| 150 | in which case your setup script would be :: |
| 151 | |
| 152 | from packaging.core import setup |
| 153 | setup(name='foobar', |
| 154 | version='1.0', |
| 155 | package_dir={'foobar': ''}, |
| 156 | packages=['foobar']) |
| 157 | |
| 158 | (The empty string also stands for the current directory.) |
| 159 | |
| 160 | If you have sub-packages, they must be explicitly listed in :option:`packages`, |
| 161 | but any entries in :option:`package_dir` automatically extend to sub-packages. |
| 162 | (In other words, the Distutils does *not* scan your source tree, trying to |
| 163 | figure out which directories correspond to Python packages by looking for |
| 164 | :file:`__init__.py` files.) Thus, if the default layout grows a sub-package:: |
| 165 | |
| 166 | <root>/ |
| 167 | setup.py |
| 168 | foobar/ |
| 169 | __init__.py |
| 170 | foo.py |
| 171 | bar.py |
| 172 | subfoo/ |
| 173 | __init__.py |
| 174 | blah.py |
| 175 | |
| 176 | then the corresponding setup script would be :: |
| 177 | |
| 178 | from packaging.core import setup |
| 179 | setup(name='foobar', |
| 180 | version='1.0', |
| 181 | packages=['foobar', 'foobar.subfoo']) |
| 182 | |
| 183 | (Again, the empty string in :option:`package_dir` stands for the current |
| 184 | directory.) |
| 185 | |
| 186 | |
| 187 | .. _packaging-single-ext: |
| 188 | |
| 189 | Single extension module |
| 190 | ======================= |
| 191 | |
| 192 | Extension modules are specified using the :option:`ext_modules` option. |
| 193 | :option:`package_dir` has no effect on where extension source files are found; |
| 194 | it only affects the source for pure Python modules. The simplest case, a |
| 195 | single extension module in a single C source file, is:: |
| 196 | |
| 197 | <root>/ |
| 198 | setup.py |
| 199 | foo.c |
| 200 | |
| 201 | If the :mod:`foo` extension belongs in the root package, the setup script for |
| 202 | this could be :: |
| 203 | |
| 204 | from packaging.core import setup, Extension |
| 205 | setup(name='foobar', |
| 206 | version='1.0', |
| 207 | ext_modules=[Extension('foo', ['foo.c'])]) |
| 208 | |
| 209 | If the extension actually belongs in a package, say :mod:`foopkg`, then |
| 210 | |
| 211 | With exactly the same source tree layout, this extension can be put in the |
| 212 | :mod:`foopkg` package simply by changing the name of the extension:: |
| 213 | |
| 214 | from packaging.core import setup, Extension |
| 215 | setup(name='foobar', |
| 216 | version='1.0', |
| 217 | packages=['foopkg'], |
| 218 | ext_modules=[Extension('foopkg.foo', ['foo.c'])]) |
| 219 | |
| 220 | |
| 221 | Checking metadata |
| 222 | ================= |
| 223 | |
| 224 | The ``check`` command allows you to verify if your project's metadata |
| 225 | meets the minimum requirements to build a distribution. |
| 226 | |
| 227 | To run it, just call it using your :file:`setup.py` script. If something is |
| 228 | missing, ``check`` will display a warning. |
| 229 | |
| 230 | Let's take an example with a simple script:: |
| 231 | |
| 232 | from packaging.core import setup |
| 233 | |
| 234 | setup(name='foobar') |
| 235 | |
| 236 | .. TODO configure logging StreamHandler to match this output |
| 237 | |
| 238 | Running the ``check`` command will display some warnings:: |
| 239 | |
| 240 | $ python setup.py check |
| 241 | running check |
| 242 | warning: check: missing required metadata: version, home_page |
| 243 | warning: check: missing metadata: either (author and author_email) or |
| 244 | (maintainer and maintainer_email) must be supplied |
| 245 | |
| 246 | |
| 247 | If you use the reStructuredText syntax in the ``long_description`` field and |
| 248 | `Docutils <http://docutils.sourceforge.net/>`_ is installed you can check if |
| 249 | the syntax is fine with the ``check`` command, using the ``restructuredtext`` |
| 250 | option. |
| 251 | |
| 252 | For example, if the :file:`setup.py` script is changed like this:: |
| 253 | |
| 254 | from packaging.core import setup |
| 255 | |
| 256 | desc = """\ |
| 257 | Welcome to foobar! |
| 258 | =============== |
| 259 | |
| 260 | This is the description of the ``foobar`` project. |
| 261 | """ |
| 262 | |
| 263 | setup(name='foobar', |
| 264 | version='1.0', |
| 265 | author=u'Tarek Ziadé', |
| 266 | author_email='tarek@ziade.org', |
| 267 | summary='Foobar utilities' |
| 268 | description=desc, |
| 269 | home_page='http://example.com') |
| 270 | |
| 271 | Where the long description is broken, ``check`` will be able to detect it |
| 272 | by using the :mod:`docutils` parser:: |
| 273 | |
| 274 | $ python setup.py check --restructuredtext |
| 275 | running check |
| 276 | warning: check: Title underline too short. (line 2) |
| 277 | warning: check: Could not finish the parsing. |
| 278 | |
| 279 | |
| 280 | .. _packaging-reading-metadata: |
| 281 | |
| 282 | Reading the metadata |
| 283 | ==================== |
| 284 | |
| 285 | The :func:`packaging.core.setup` function provides a command-line interface |
| 286 | that allows you to query the metadata fields of a project through the |
| 287 | :file:`setup.py` script of a given project:: |
| 288 | |
| 289 | $ python setup.py --name |
| 290 | foobar |
| 291 | |
| 292 | This call reads the ``name`` metadata by running the |
| 293 | :func:`packaging.core.setup` function. When a source or binary |
| 294 | distribution is created with Distutils, the metadata fields are written |
| 295 | in a static file called :file:`PKG-INFO`. When a Distutils-based project is |
| 296 | installed in Python, the :file:`PKG-INFO` file is copied alongside the modules |
| 297 | and packages of the distribution under :file:`NAME-VERSION-pyX.X.egg-info`, |
| 298 | where ``NAME`` is the name of the project, ``VERSION`` its version as defined |
| 299 | in the Metadata, and ``pyX.X`` the major and minor version of Python like |
| 300 | ``2.7`` or ``3.2``. |
| 301 | |
| 302 | You can read back this static file, by using the |
| 303 | :class:`packaging.dist.Metadata` class and its |
| 304 | :func:`read_pkg_file` method:: |
| 305 | |
| 306 | >>> from packaging.metadata import Metadata |
| 307 | >>> metadata = Metadata() |
| 308 | >>> metadata.read_pkg_file(open('distribute-0.6.8-py2.7.egg-info')) |
| 309 | >>> metadata.name |
| 310 | 'distribute' |
| 311 | >>> metadata.version |
| 312 | '0.6.8' |
| 313 | >>> metadata.description |
| 314 | 'Easily download, build, install, upgrade, and uninstall Python packages' |
| 315 | |
| 316 | Notice that the class can also be instantiated with a metadata file path to |
| 317 | loads its values:: |
| 318 | |
| 319 | >>> pkg_info_path = 'distribute-0.6.8-py2.7.egg-info' |
| 320 | >>> Metadata(pkg_info_path).name |
| 321 | 'distribute' |
| 322 | |
| 323 | |
| 324 | .. XXX These comments have been here for at least ten years. Write the |
| 325 | sections or delete the comments (we can maybe ask Greg Ward about |
| 326 | the planned contents). (Unindent to make them section titles) |
| 327 | |
| 328 | .. multiple-ext:: |
| 329 | |
| 330 | Multiple extension modules |
| 331 | ========================== |
| 332 | |
| 333 | Putting it all together |
| 334 | ======================= |