Skip to main content

Utilities for building and installing Astropy, Astropy affiliated packages, and their respective documentation.

Project description


.. image::

.. image::

.. image::

**Warning:** Please note that version ``v3.0`` and later of ``astropy-helpers``
requires Python 3.5 or later. If you wish to maintain Python 2 support
for your package that uses ``astropy-helpers``, then do not upgrade the
helpers to ``v3.0+``. We will still provide Python 2.7 compatible
releases on the ``v2.0.x`` branch during the lifetime of the ``astropy``
core package LTS of ``v2.0.x``.


This project provides a Python package, ``astropy_helpers``, which includes
many build, installation, and documentation-related tools used by the Astropy
project, but packaged separately for use by other projects that wish to
leverage this work. The motivation behind this package and details of its
implementation are in the accepted
`Astropy Proposal for Enhancement (APE) 4 <>`_.

``astropy_helpers`` includes a special "bootstrap" module called
```` which is intended to be used by a project's in
order to ensure that the ``astropy_helpers`` package is available for

As described in `APE4 <>`_, the version
numbers for ``astropy_helpers`` follow the corresponding major/minor version of
the `astropy core package <>`_, but with an independent
sequence of micro (bugfix) version numbers. Hence, the initial release is 0.4,
in parallel with Astropy v0.4, which will be the first version of Astropy to
use ``astropy-helpers``.

For examples of how to implement ``astropy-helpers`` in a project,
see the ```` and ``setup.cfg`` files of the
`Affiliated package template <>`_.

What does astropy-helpers provide?

Astropy-helpers' big-picture purpose is to provide customization to Python's
packaging infrastructure process in ways that the Astropy Project has found to
help simplifying the developint and releasing packages. This is primarily
build around ```` commands, as outlined below, as well as code to help
manage version numbers and better control the build process of larger packages.

Custom commands

The main part of astropy-helpers is to provide customized setuptools commands.
For example, in a package that uses astropy-helpers, the following command
will be available::

python build_docs

and this command is implemented in astropy-helpers. To use the custom
commands described here, add::

from astropy_helpers.setup_helpers import register_commands

to your ```` file, then do::

cmdclassd = register_commands(NAME, VERSION, RELEASE)

where ``NAME`` is the name of your package, ``VERSION`` is the full version
string, and ``RELEASE`` is a boolean value indicating whether the version is
a stable released version (``True``) or a developer version (``False``).
Finally, pass ``cmdclassd`` to the ``setup`` function::


The commands we provide or customize are:

**python test**

This command will automatically build the package, install it to a temporary
directory, and run the tests using `pytest <>`_ on this
installed version. Note that the bulk of this command is actually defined
in ``astropy.tests.command.AstropyTest``, because that allows the test
machinery to operate outside a setuptools command. This, here we
simply define the custom
setuptools command.

**python sdist**

We redefine ``sdist`` to use the version from distutils rather than from
setuptools, as the setuptools version requires duplication of information
in ````.

**python build_docs**

This command will automatically build the package, then run sphinx to build
the documentation. This makes development much easier because it ensures
sphinx extensions that use the package's code to make documentation are
actually using the in-development version of the code. Sphinx itself
provides a custom setuptools command, which we
expand with the following options:

* ``-w``: set the return code to 1 if there are any warnings during the build

* ``-l``: completely clean previous builds, including files generated by
the sphinx-automodapi package (which creates API pages for different

* ``-n``: disable the intersphinx option.

* ``-o``: open the documentation in a browser if a build finishes successfully.

In addition, ``build_docs`` will automatically download and temporarily install
sphinx-astropy (which is a meta-package that
provides standardized configuration and documentation dependencies for astropy
packages) if it isn't already installed. Temporary installation means that the
package will be installed into an ``.eggs`` directory in the current working
directory, and it will only be available for the duration of the call to

**python build_ext**

This is also used when running ``build`` or ``install``. We add several features
compared to the default ``build_ext`` command:

* For packages with C/Cython extensions, we create a ``packagename._compiler``
submodule that contains information about the compilers used.

* Packages that need to build C extensions using the Numpy C API, we allow
those packages to define the include path as ``'numpy'`` as opposed to having
to import Numpy and call ``get_include``. The goal is to solve the issue that
if one has to import Numpy to define extensions, then Numpy has to be
installed/available before the package is installed, which means that one
needs to install Numpy in a separate installation step.

* We detect broken compilers and replace them with other compilers on-the-fly
unless the compiler is explicitly specified with the ``CC`` environment

* If Cython is not installed, then we automatically check for generated C files
(which are normally present in the stable releases) and give a nice error
if these are not found.

Version helpers

Another piece of functionality we provide in astropy-helpers is the ability
to generate a ``packagename.version`` file that includes functions that
automatically set the version string for developer versions, to e.g.
``3.2.dev22213`` so that each developer version has a unique number (although
note that branches an equal number of commits away from the master branch will
share the same version number). To use this, import::

from astropy_helpers.git_helpers import get_git_devstr

in your ```` file, and you will then be able to use::

VERSION += get_git_devstr()

where ``VERSION`` is a version string without any developer version suffix.

We then also provide a function that generates a ```` file inside your
package (which can then be imported as ``packagename.version``) that contains
variables such as ``major``, ``minor``, and ``bugfix``, as well as
``version_info`` (a tuple of the previous three values), a ``release`` flag that
indicates whether we are using a stable release, and several other complementary
variables. To use this, import::

from astropy_helpers.version_helpers import generate_version_py

in your ```` file, and call::

generate_version_py(NAME, VERSION, RELEASE, uses_git=not RELEASE)

where ``NAME`` is the name of your package, ``VERSION`` is the full version string
(including any developer suffix), ``RELEASE`` indicates whether the version is a
stable or developer version, and ``uses_git`` indicates whether we are in a git
repository (using ``not RELEASE`` is sensible since git is not available in a
stable release).

Collecting package information

The ``setup`` function from setuptools can take a number of options that indicate
for example what extensions to build, and what package data to include. However,
for large packages this can become cumbersome. We therefore provide a mechanism
for defining extensions and package data inside individual sub-packages. To do
this, you can create ```` files anywhere in your package, and
these files can include one or more of the following functions:

* ``get_package_data``:
This function, if defined, should return a dictionary mapping the name of
the subpackage(s) that need package data to a list of data file paths
(possibly including wildcards) relative to the path of the package's source
code. e.g. if the source distribution has a needed data file
``astropy/wcs/tests/data/3d_cd.hdr``, this function should return
``{'astropy.wcs.tests':['data/3d_cd.hdr']}``. See the ``package_data``
option of the :func:`distutils.core.setup` function.

It is recommended that all such data be in a directory named ``data`` inside
the package within which it is supposed to be used. This package data
should be accessed via the ```` and
```` functions.

* ``get_extensions``:
This provides information for building C or Cython extensions. If defined,
it should return a list of ``distutils.core.Extension`` objects.

* ``get_build_options``:
This function allows a package to add extra build options. It
should return a list of tuples, where each element has:

- *name*: The name of the option as it would appear on the
commandline or in the ``setup.cfg`` file.

- *doc*: A short doc string for the option, displayed by
`` build --help``.

- *is_bool* (optional): When `True`, the option is a boolean
option and doesn't have an associated value.

Once an option has been added, its value can be looked up using

* ``get_external_libraries``:
This function declares that the package uses libraries that are
included in the astropy distribution that may also be distributed
elsewhere on the users system. It should return a list of library
names. For each library, a new build option is created,
``'--use-system-X'`` which allows the user to request to use the
system's copy of the library. The package would typically call
``astropy_helpers.setup_helpers.use_system_library`` from its
``get_extensions`` function to determine if the package should use
the system library or the included one.

* ``get_entry_points()``:
This function can returns a dict formatted as required by
the ``entry_points`` argument to ``setup()``.

With these files in place, you can then add the following to your ````

from astropy_helpers.setup_helpers import get_package_info


package_info = get_package_info()


setup(..., **package_info)

OpenMP helpers

We provide a helper function ``add_openmp_flags_if_available`` that can be used
to automatically add OpenMP flags for C/Cython extensions, based on whether
OpenMP is available and produces executable code. To use this, edit the
```` file where you define a C extension, import the helper

from astropy_helpers.openmp_helpers import add_openmp_flags_if_available

then once you have defined the extension and before returning it, use it as::

extension = Extension(...)


return [extension]

Using astropy-helpers

The easiest way to get set up with astropy-helpers in a new package is to use
the `package-template <>`_
that we provide. This template is specifically designed for use with the helpers,
so using it avoids some of the tedium of setting up the heleprs.

However, we now go through the steps of adding astropy-helpers
as a submodule to a package in case you wish to do so manually. First, add
astropy-helpers as a submodule at the root of your repository::

git submodule add git:// astropy_helpers

Then go inside the submodule and check out a stable version of astropy-helpers.
You can see the available versions by running::

$ cd astropy_helpers
$ git tag

If you want to support Python 2, pick the latest v2.0.x version (in the above
case ``v2.0.7``) and if you don't need to support Python 2, just pick the latest
stable version (in the above case ``v3.0.2``). Check out this version with e.g.::

$ git checkout v3.0.2

Then go back up to the root of your repository and copy the ````
file from the submodule to the root of your repository::

$ cd ..
$ cp astropy_helpers/ .

Finally, add::

import ah_bootstrap

at the top of your ```` file. This will ensure that ``astropy_helpers``
is now available to use in your ```` file. Finally, add then commit your

git add astropy_helpers
git commit -m "Added astropy-helpers"

Updating astropy-helpers

If you would like the Astropy team to automatically open pull requests to update
astropy-helpers in your package, then see the instructions `here

To instead update astropy-helpers manually, go inside the submodule and do::

cd astropy_helpers
git fetch origin

Then checkout the version you want to use, e.g.::

git checkout v3.0.3

Go back up to the root of the repository and update the ```` file
too, then add your changes::

cp astropy_helpers/ .
git add astropy_helpers
git commit ...

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Filename, size & hash SHA256 hash help File type Python version Upload date
astropy-helpers-3.1.tar.gz (61.3 kB) Copy SHA256 hash SHA256 Source None

Supported by

Elastic Elastic Search Pingdom Pingdom Monitoring Google Google BigQuery Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN SignalFx SignalFx Supporter DigiCert DigiCert EV certificate StatusPage StatusPage Status page