What is the easiest way to make an optional C extension for a python package?
Asked Answered
P

2

15

I've created a C extension that I'd like to enable in my Python package (using setuptools) only if a command line option is passed in. What is the easiest way to do this?

I can't seem to find any straightforward ways of going about this.

Proulx answered 30/10, 2010 at 0:11 Comment(0)
L
4
ext_modules = []
if '--add-this' in sys.argv:
    ext_modules.append(Extension(...))
    sys.argv.remove('--add-this')
setup(...
      ext_modules = ext_modules
)

This is hacky, but might be easiest. A more advanced approach would be to extend the Distribution class to support a flag, say --with-modules and then customize ext_modules inside finalize_options.

Laurenlaurena answered 30/10, 2010 at 1:21 Comment(0)
D
15

There's actually a distribute/setuptools feature called "Features" that can be used for this. It's explicitly designed to have setup.py do different things based on --with-xxx and --without-xxx command line options.

  • This blog post gives a nice introduction, I can't find any better documentation at this time (besides the Distribute source - the Feature class and features keyword).
  • The jinja project's setup.py uses Features for your exact purpose, it might be a good template to work from.
  • The simplejson setup.py also does something similar, except that it's coded to always try to build the C-extension feature it defines, and fall back gracefully to pure-python when building fails; this may also be useful for your purpose.
Diastole answered 20/4, 2011 at 19:28 Comment(1)
The lack of documentation for Feature might indicate their "essential deprecation - an experimental thing that turned out to be, well, a failed experiment": mail-archive.com/[email protected]/msg12061.htmlOrdonnance
L
4
ext_modules = []
if '--add-this' in sys.argv:
    ext_modules.append(Extension(...))
    sys.argv.remove('--add-this')
setup(...
      ext_modules = ext_modules
)

This is hacky, but might be easiest. A more advanced approach would be to extend the Distribution class to support a flag, say --with-modules and then customize ext_modules inside finalize_options.

Laurenlaurena answered 30/10, 2010 at 1:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.