python setup.py sdist and custom setup keywords don't play together
<h2>subtitle: Not only sdist</h2>

<p>I am trying to get the <code>setup.py</code> file of a package I'm working on to play nicely with <code>sdist</code>. The relevant parts of the <code>setup.py</code> file are:</p>

<pre><code>from setuptools.command.test import test
[...]
class Tox(test):
   "as described in 
    http://tox.readthedocs.org/en/latest/example/basic.html?highlight=setuptools#integration-with-setuptools-distribute-test-commands"
   [...]
def run_tests(self):
    if self.distribution.install_requires:
        self.distribution.fetch_build_eggs(
            self.distribution.install_requires)
    if self.distribution.tox_requires:
        self.distribution.fetch_build_eggs(self.distribution.tox_requires)
    # import here, cause outside the eggs aren't loaded
    import tox
    import shlex
    args = self.tox_args
    if args:
        args = shlex.split(self.tox_args)
    else:
        args = ""
    errno = tox.cmdline(args=args)
    sys.exit(errno)


entry_points ={}
distutils_ext = {'distutils.setup_keywords': [
                    "tox_requires = setuptools.dist:check_requirements", ]
                 }
entry_points.update(distutils_ext)

setup(
      install_requires=['six', 'numpy', 'matplotlib', 'scipy', 'astropy&gt;=1',
                  'Pillow', ],

    cmdclass={
        'test': PyTest,  # this is to run python setup.py test
        'tox': Tox,
    },

    # list of packages and data
    packages=find_packages(),

    # tests
    tests_require=['pytest', 'pytest-cov'],
    tox_requires=['tox'],
    # other keywords, mostly metadata
)
</code></pre>

<p>If I run <code>python setup.py sdist</code>, I get a warning at the beginning: </p>

<pre><code>/usr/lib/python2.7/distutils/dist.py:267: UserWarning: Unknown distribution option: 'tox_requires'
  warnings.warn(msg)
</code></pre>

<p>but then the sdist works fine and it creates a tar.gz file that I can use to install my package.</p>

<p>But if I run it a second time, it starts with (it's the beginning of Pillow building):</p>

<pre><code>warning: no previously-included files found matching '.editorconfig'
Building using 4 processes
_imaging.c: In function ‘getink’:
</code></pre>

<p>and begins building all the required packages into .eggs directory.</p>

<p>If I remove the *egg-info directory I can rerun the command.
If I comment out the <code>tox_requires=[...]</code> line, I can build the sdist as many times as I want.</p>

<p>Now according to the <a href="https://pythonhosted.org/setuptools/setuptools.html#adding-setup-arguments" rel="nofollow">setuptools documentation</a> the command above should be the correct way to run add new arguments to the setup function.</p>

<hr>

<p>As per the subtitle, the problem is not only with <code>sdist</code> but it's probably due to my non-understanding on how setuptools and requirements work.</p>

<p>If I run <code>python setup.py tox</code> in a place without tox already installed I get, after the installation of some testing package it should not install (namely <code>pytest</code> and <code>pytest-cov</code>):</p>

<p>Traceback (most recent call last):
   [...]
      File "/usr/lib/python2.7/dist-packages/setuptools/command/test.py", line 127, in with_project_on_sys_path
        func()
      File "setup.py", line 65, in run_tests
        if self.distribution.tox_requires:
    AttributeError: Distribution instance has no attribute 'tox_requires'</p>

<hr>

<p>[Update] The <code>tox_requires</code> also confuse very badly pip during installation. If it is commented out I can install the package without any issue; otherwise it begins to compile the source of the packages and it systematically fails because it doesn't find <code>numpy</code> while building stuff like <code>scipy</code></p>

<hr>

<p>How can I get setuptools to recognize and properly use <code>tox_requires</code>?</p>

<p>Once this issue is fixed, I think that I can get rid of the spurious installations here doing a better job at implementing the <code>Tox</code> class, maybe overriding more things from <code>test</code> or deriving it directly from <code>Command</code></p>