18

The python doc for "Writing the Setupscript (http://docs.python.org/2/distutils/setupscript.html) mentions that dependencies can be specified under section

> 2.4. Relationships between Distributions and Packages

[...] These relationships can be specified using keyword arguments to the distutils.core.setup() function.

Dependencies on other Python modules and packages can be specified by supplying the requires keyword argument to setup(). The value must be a list of strings. Each string specifies a package that is required, and optionally what versions are sufficient.

To specify that any version of a module or package is required, the string should consist entirely of the module or package name. Examples include 'mymodule' and 'xml.parsers.expat'.

[...]

Given this rather sparse information without an example I just want to make sure that I do it right. Also, I cannot find this requires parameter in the API description http://docs.python.org/2/distutils/apiref.html#distutils.core.setup

So is it done like this,e.g.,

setup(name='MyStuff',
      version='1.0',
      requires='os, sys, progressbar',
      [...]

I hope some one can give me a little bit more insight! Thanks!

EDIT:

To address the distutils.core, setuptools controversy, one could simply do

try:
    from setuptools import setup
except ImportError:
    from distutils.core import setup

Does it make sense?

5
  • 1
    The whole Python packaging system is badly documented, mainly because there are so many different places with partly very good, but contradictory information. I always look at existing projects. In case you do not have special needs, you only need distutils. For example bottleneck has is a good example for a setup.py file that only used distutils: github.com/kwgoodman/bottleneck/blob/master/setup.py Commented Jul 18, 2013 at 15:19
  • @Jan-PhilipGehrcke: Yet bottleneck fails to specify that it requires numpy in the metadata. setuptools has much better documentation these days, thanks to the distribute fork being merged back: pythonhosted.org/setuptools Commented Jul 18, 2013 at 15:33
  • Right you are, it even starts with importing numpy right away. Commented Jul 18, 2013 at 15:34
  • that's a very nice example, I will save it to my references! Commented Jul 18, 2013 at 15:34
  • I think a good solution would be to have something like try: from setuptools import setup except ImportError: from distutils.core import setup (I will add it to my question, the formatting is just ugly here in the comments) Commented Jul 18, 2013 at 15:53

1 Answer 1

20

Ignore distutils. If you want to create a package that specifies dependencies for a tool like pip to go out and find for you, you need to base your setup.py of off setuptools instead.

setuptools dependencies are listed in install_requires, which takes a list:

setup(name='MyStuff',
      version='1.0',
      install_requires=['progressbar'],
      # ...
)

which should be distributions of their own. os and sys are modules included with Python and should not be listed.

Sign up to request clarification or add additional context in comments.

2 Comments

I recently had a longer discussion about this and got convinced that in the current situation a good idiom is try: from setuptools import setup; except ImportError: from distutils.core import setup.
@Jan-PhilipGehrcke Just a FYI that setup from distutils.core does not support install_requires. So if you attempt to use it, you will see a message like this: /usr/lib/python3.4/distutils/dist.py:260: UserWarning: Unknown distribution option: 'install_requires'. So in light of this and other differences, I don't think that's a very good idiom under anything but the most straightforward circumstances.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.