Best way to share code across several setup.py scripts?

python, setuptools

Solution

Normally `setup.py` is the entry point for distribution of distinct packages. As such, it's hard to then share code between those packages.

If you use `setuptools` (or it's fork, `distribute`) in your `setup.py`, you can specify packages that must be installed when installing your package with the `setup_requires` entry.

Unfortunately, your `setup.py` is executed first; as soon as the `setup_requires` line is parsed, the extra packages listed there will be installed locally, but this may be too late for your needs.

The work-around is to create a separate `Distribution` object before you call `setup` which defines the `setup_requires` entries:

import setuptools

setuptools.dist.Distribution(dict(setup_requires='yoursharedsetuppackage'))
# `setup_requires` is parsed and acted upon immediately; from here on out
# the yoursharedsetuppackage is installed and importable.

import yoursharedsetuppackage

setup(...)

Problem

I've got several packages I'm working on, and I'd like to share code between their setup.py scripts. Is there any good way to do this or is code duplication my only option?

Original source