Search code examples
pythonpython-2.7python-3.xsetuptoolsdistutils

Is it safe to call `setup()` multiple times in a single `setup.py`?


I am developing a package containing Cython extensions.

According to https://github.com/pypa/pip/issues/1958 I shall use setup_requires and postpone import of Cython. The best solution I came up with is to call setup() twice in setup.py:

... # initial imports
setup(setup_requires=['cython'])
from Cython.Build import cythonize
bar = Extension('foo.bar', sources = ['bar.pyx'])
setup(name = 'foo',
      ... # parameters
      ext_modules = cythonize([bar]),
      ... # more parameters
      )

However I have a feeling that the name of setup() suggests it shall be called only once. Is it safe to call it several times like I do?

I can not just distribute wheels because the package shall be available also for Linux users.

[EDIT]

Also I see the question as more general than dealing with compiler-dependencies. One may want to import some package (eg. sphinx or pweave) to preprocess the description of ones package.


Solution

  • The simple answer is: No. Once you call setup, it will parse the command line arguments and start doing its job.

    As for Cython dependency, setup_requires cannot help here. It will probably try to download Cython without installing. As SpotlightKid commented:

    distutils doesn't try to be a compiler or install gcc as a dependency either

    According to the setuptools

    this argument (setup_requires) is needed if you are using distutils extensions,

    and thus, not for packages like Cython.

    I think the user is responsible to install Cython before calling setup.py. If you want to provide more friendly error message, try to use

    try:
        from Cython.Build import cythonize
    except ImportError:
        # Kindly ask the user to install Cython
    

    The following posts may help: