tl.pkg - A template for a namespaced Python package with Sphinx docs

This package generates the basic file and directory layout of Python packages with Sphinx documentation and a development buildout. It consists of two parts:

  • a paste.script template that creates the boilerplate for a Python package that lives in one level of namespace, and
  • a Python module that is used to configure Sphinx, along with the necessary package dependencies and some theming.

The package works with Python 2.6 and 2.7.


To make the paster template available, install tl.pkg where paster can find it. Then run paster:

$ paster create --template tl-pkg <NAMESPACE>.<PROJECTNAME>

This will generate the boilerplate for an egg distribution, complete with zc.buildout configuration, the skeleton of Sphinx package documentation, and a Mercurial repository initialised. The buildout configuration is targeted at development, so it will install a testrunner at bin/test and a documentation builder at bin/doc.

A few variables will be prompted for, among them a one-line description and some keywords for the package.


Three more variables that paster asks you for are used to personalise the package skeleton it will generate. These variables may have default values that are read from a file named $HOME/.tl-pkg.cfg if it exists. The file needs to follow ini-file syntax as understood by Python's ConfigParser and contain one section (with an arbitrary name so far) that defines any of the following variables:

author:Your full name. This will appear in the package metadata and documentation as well as in the copyright notices of any Python files generated.
author-email:Your e-mail address. This appears both in the package metadata and documentation.
bitbucket-name:Your bitbucket user name. This is used to construct the various URLs belonging to the project. At present, the assumption is that the project is hosted at <http://bitbucket.org/> and any URLs in the package metadata and documentation point to appropriate pages of that bitbucket project.

Package contents

This is to explain the purpose of the generated files and directories, along with advice on which files to edit when. Many files will not need to be edited at all.

Python distribution

setup.py:The package definition and metadata. Update this file at least whenever the package's version number, dependencies, entry points change.
<NAMESPACE>:The source code tree of the package. Don't modify the namespace package's __init__.py file lest other packages in the same namespace cannot be imported.

Mercurial repository

.hg:The Mercurial repository is already initialised when the package has been created. The generated files have not been committed yet.
.hg/hgrc:Repository configuration that points to the future URL of the package in some Mercurial hosting, if any. It also sets your hg user name.
.hgignore:Files and directories to be ignored by Mercurial. This includes local configuration and stuff expected to be generated by buildout, documentation builds or package releases. It does not include files generated by Python (such as *.pyc), distribute (*.egg-info), or other more general tools like your editor, which are not specific to this project. Such patterns should be on your default Mercurial ignore list.

Development buildout

bootstrap.py:Creates the bin/buildout script. Run this with the same Python interpreter that buildout should use. No need to ever edit this file.
buildout.cfg:A working buildout configuration that creates a test runner and a documentation builder for the package. The package itself is included as a develop egg and buildout is configured to use only pinned versions of any other packages. Edit this to configure the package's official development buildout but put local customisations in local.cfg. Version pinnings go in versions/versions.cfg while this file's versions section should only undo pinnings of packages that are declared develop eggs by this same file's buildout section.
local.cfg:Local customisations of the buildout configuration that are of no interest to other developers. This is being ignored by Mercurial. If you change this file, run bin/buildout -c local.cfg from then on. While this may sound cumbersome at first, keeping the non-local configuration in buildout.cfg and under version control is important for use cases such as testing the package on a continuous-integration server.
 Version pinning for any packages used by the buildout that are not part of the Zope toolkit. The version of tl.pkg which is required for building the documentation is pinned to the same version that created the package files. When upgrading tl.pkg later, this version pinning needs to be updated along with any files that have changed in the package template between the versions. Edit this file to pin the versions of any eggs required by your package or your buildout.
 A fixed release of the Zope toolkit, included in our version pinnings. Keeping a local copy of this allows building the buildout without network access. Do not edit this file.

General package documentation

There are a number of text files to be found in the package's top-level directory that contain standard pieces of the documentation and are therefore expected in that place and under their particular names, and which need to be accessible independent of Sphinx. These files need to be valid restructured text as they are being processed by Sphinx when building the full documentation, except for the copyright notice and licence text which are included verbatim.

README.txt:An overview of the package's purpose, contents and usage which will be part of its PyPI page and of the documentation's index page. This should be kept up-to-date with the package contents at all times.
CHANGES.txt:The change log that needs to be updated with any changes to the package that are relevant to the users of the package. The file's format is understood by zest.releaser and the current version of it (i.e. the "tip" version in the public Mercurial repository) will be pointed to from the PyPI page and the built package documentation.
ABOUT.txt:Some pointers about the package and its authors, such as the latter's e-mail address and the URLs of the package's documentation, PyPI page, issue tracker and source code as well as the current log. It is assumed that documentation will be be published both at PyPI and at <http://readthedocs.org/>; you should make sure to use the correct respective URLs assigned to your project.
COPYRIGHT.txt:Copyright information for the package: copyright holder including the copyright years and some advice about the licence used, which is the Zope public licence, version 2.1 by default. Edit this at least to update the years.
LICENSE.txt:A copy of the official text of the licence used. Do not edit this except to exchange it for a different licence.

Full documentation, built using Sphinx

doc:Everything that is only relevant to the Sphinx-generated documentation. We use the suffix .txt for Sphinx input files. While a number of conventions exist for the contents of the doc directory, nothing bad will happen to the rest of the package if you modify it freely; just make sure it remains valid Sphinx input.
doc/conf.py:Sphinx configuration. Basically all of the configuration values follow conventions and are therefore imported from tl.pkg, so you must keep the import and invocation of tl.pkg.sphinxconf intact. You'll have to edit this file if you want to change something about the metadata or the appearance of the documentation just for this package. Updates to the conventions for Sphinx-generated documentation will be acquired by upgrading tl.pkg.
doc/index.txt:The front page of the documentation. It includes the package overview from the top-level README.txt file and a table of contents pointing to the sections of the full documentation. These include generated API documentation, some meta information about the package and the change log. Edit this file if you want to add top-level sections, for example.
 The root document of the narrative package documentation. This is intended to collect any doc-test files that reside among the Python modules in your source tree. You need to list the files under the toctree directive, their document names being of the pattern <NAMESPACE>.<PACKAGENAME>-<FILENAME> (without the .txt suffix). A commented-out example file listing is included.
doc/api.txt:The root document of the generated API documentation. The API is documented semi-automatically in that you have to list in this file, under the autosummary directive, all the modules to be documented, which happens automatically from then on. A commented-out example module listing is included.
 A stub to include the top-level file README.txt. No need to edit this file.
doc/about.txt:Meta information about the package, combining the top-level files ABOUT.txt, COPYRIGHT.txt, and LICENSE.txt. You will not need to edit this file.
 A stub to include the top-level file CHANGES.txt. No need to edit this file.
 A listing of Python eggs (other than Sphinx itself) required to build the documentation. This is meant for building the documentation at <http://readthedocs.org/>. You'll need to be whitelisted with them in order to be able to use the conventions implemented by tl.pkg. Edit this file whenever your documentation's package dependencies change; you cannot use egg extras here.

Building the full documentation

The generated buildout configuration installs a script at bin/doc that calls Sphinx to build the documentation. To run this script, your current working directory must be the package root. The script will put the built documentation into build/doc/ (relative to the package's top-level directory). Options passed to bin/doc will be passed on to the underlying sphinx-build command, but note that positional arguments won't work.

Sphinx configuration values

By default, a number of Sphinx extensions is enabled, so you might want to configure these in addition to the core Sphinx variables:

  • sphinx.ext.autosummary
  • sphinx.ext.viewcode
  • sphinx.ext.inheritance_diagram
  • sphinxcontrib.cheeseshop
  • sphinxcontrib.issuetracker

You can override the defaults from tl.pkg by simply setting the respective variables in your conf.py. The invocation of tl.pkg.sphinxconf.set_defaults needs to happen at the end:

source_suffix = '.foo'

import tl.pkg.sphinxconf

Conversely, sphinxconf tries to use variables from conf.py to calculate values. If these variables are specified, that must also be done before set_defaults is called. Currently, the following variables are recognised:

_year_started:Optional value for the year the project was started. This defaults to the current year (at the time of documentation building), but if it is specified and different from the current year, it is used to construct a copyright notice like "2001-2012 Author".
_flattr_url:If specified, this is assumed to be the URL of a flattr thing for this project and flattr donation buttons will appear at the top of the menu column of the full documentation. To add a flattr button to the PyPI page, uncomment the "Support the project" item in ABOUT.txt and fill in the URL there as well.
 If set to a true value, the bitbucket integration of the sphinxcontrib-issuetracker integration will be modified so that it won't try to access the <http://bitbucket.org/> server when building the documentation and the Sphinx run remains independent of network access. (Integration with other trackers hasn't been taken care of so far.) This will disable some functionality of the tracker integration but retain, e.g., the issuetracker extension's ability to recognise plain-text issue numbers.

Finally, the tl.pkg.sphinxconf module defines a function that you may call to register mock modules if the documentation is to be built on a system such as <http://readthedocs.org/> that cannot install certain code (like modules implemented in C):

tl.pkg.sphinxconf.register_mock_modules('cairo', 'gobject', 'gtk')

Known issues

There is a known issue with project names that contain underscores. Due to the way tl.pkg infers metadata such as the project and package name from the package's egg info, names with underscores would by default get their underscores replaced by dashes, breaking the documentation build. To use underscores in your project's name, explicitly repeat the name in conf.py:


import tl.pkg.sphinxconf