<div dir="ltr"><div>Hi all,</div><div><br></div><div>The `setup_requires` option to `setup()` is well-known to suffer from multiple issues.  Most importantly, as it is a keyword argument to `setup()`, it appears too late for modules that may need to be imported for the build to occur (e.g., Cython, for which support must explicitly provided by setuptools itself rather than by letting Cython hook into it); additionally, there are various contorsions that people go to to avoid some `setup_requires` when not building the package (such as checking the value of `sys.argv`).  `setup_requires` also uses `easy_install` rather than `pip`, but I do not see why this could not be fixed; let's focus on the first issue instead.</div><div><br></div><div>If `setup_requires` appears too late to be useful, the obvious(?) option is to move it earlier: provide a function, say, `setuptools.setup_requires()`, that should be called *before* `setup()` itself, e.g.:</div><div><br></div><div>    from setuptools import setup, setup_requires</div><div>    setup_requires("numpy", needed_for=["build_ext"])</div><div>    try:</div><div>        import numpy as np</div><div>    except ImportError:</div><div>        np = None</div><div>    setup(..., include_dirs=[np.get_include()] if np else [])</div><div><br></div><div>When `setup.py` is invoked, either directly or by pip, upon the call to `setup_requires()`, if `sys.argv[0]` is in the `needed_for` kwarg, and at least one requirement is missing, `setup_requires()` calls asks pip to install the required packages (similarly to `<a href="https://bitbucket.org/dholth/setup-requires`">https://bitbucket.org/dholth/setup-requires`</a>) in a temporary directory, and the whole Python process replaces itself (in the `os.execv()` sense) by a new call to `python setup.py` with this temporary directory prepended to the PYTHONPATH.  In this new process, the arguments to `setup_requires()` are now available and we can proceed to the rest of `setup.py`.</div><div><br></div><div>I feel like this idea is natural enough that someone must already have come up with it... but I may be missing something :-)</div><div><br></div><div>Best,</div><div>Antony</div></div>