[Distutils] setuptools special case Pyrex and break Cython

Phillip J. Eby pje at telecommunity.com
Wed Sep 5 22:43:16 CEST 2007

At 10:21 PM 9/5/2007 +0200, Stefan Behnel wrote:

>Phillip J. Eby wrote:
> > At 09:49 PM 9/5/2007 +0200, Stefan Behnel wrote:
> >> get a build_ext replacement, which is
> >> required by both Pyrex and Cython.
> >
> > No, it isn't.  A setuptools-based project does not need to import
> > *anything* from Pyrex; it just declares a setuptools Extension() with
> > .pyx sources.
> > But this assumption
> > about replacing build_ext being necessary is clearly a big part of
> > what's leading us to different conclusions.
>Ah, I missed that part. So setuptools special cases Pyrex in other places,
>too, thanks for pointing that out.

 From the docs:

Distributing Extensions compiled with Pyrex

``setuptools`` includes transparent support for building Pyrex extensions, as
long as you define your extensions using ``setuptools.Extension``, *not*
``distutils.Extension``.  You must also not import anything from Pyrex in
your setup script.

If you follow these rules, you can safely list ``.pyx`` files as the source
of your ``Extension`` objects in the setup script.  ``setuptools`` will detect
at build time whether Pyrex is installed or not.  If it is, then ``setuptools``
will use it.  If not, then ``setuptools`` will silently change the
``Extension`` objects to refer to the ``.c`` counterparts of the ``.pyx``
files, so that the normal distutils C compilation process will occur.

Of course, for this to work, your source distributions must include the C
code generated by Pyrex, as well as your original ``.pyx`` files.  This means
that you will probably want to include current ``.c`` files in your revision
control system, rebuilding them whenever you check changes in for the ``.pyx``
source files.  This will ensure that people tracking your project in CVS or
Subversion will be able to build it even if they don't have Pyrex installed,
and that your source releases will be similarly usable with or without Pyrex.

>Maybe I should really consider adding a Pyrex replacement to Cython... but
>that needs to be discussed on the cython-dev list.
>I still wouldn't mind having a few additional if-Pyrex-import-fails-try-Cython
>lines in setuptools for now...

Well, does that need to be done for the actual build_ext command, 
too?  (See setuptools.command.build_ext)

More information about the Distutils-SIG mailing list