[Distutils] How to implement ‘setup.py’ functionality that itself needs third-party distributions

Ben Finney ben+python at benfinney.id.au
Fri Jan 23 05:37:11 CET 2015


Ben Finney <ben+python at benfinney.id.au> writes:

> How can I ‘import docutils’ in ‘setup.py’ without creating a circular
> dependency:
>
> * The correct way to get Docutils installed is to run ‘setup.py’ and
>   have it install the dependencies.
[…]
> What I need is a way to express “ensure Docutils is installed before
> continuing with other Setuptools actions” in ‘setup.py’. I don't know of
> a neat way to tell Setuptools that.

Thanks to George V. Reilly for a much neater solution:

    # setup.py

    from setuptools import (dist, setup, find_packages)

    # Declare a requirement for Docutils in a throw-away distribution.
    # Surprisingly, Setuptools will magically ensure this is installed,
    # or raise an exception if it can't.
    dist.Distribution(dict(setup_requires="docutils"))

    # By the time we get here, the following import will work.
    import docutils

    # … other code that needs Docutils …

    setup(…)

Should this be in the Setuptools documentation? My time over the past
several weeks would have been *much* less frustrating if this was the
documented One Obvious Way™ to early-load a dependency in setup.

-- 
 \       “Free thought is a necessary, but not a sufficient, condition |
  `\                                       for democracy.” —Carl Sagan |
_o__)                                                                  |
Ben Finney



More information about the Distutils-SIG mailing list