I have written a Python module, and I have two versions: a pure Python implementation and a C extension. I've written the __init__.py
file so that it tries to import the C extension, and if that fails, it imports the pure Python code (is that reasonable?).
Now, I'd like to know what is the best way to distribute this module (e.g. write setup.py
) so it can be easily used by people with or without the facility to build, or use, the C extension, just by running:
python setup.py install
My experience is limited, but I see two possible cases:
(is that reasonable?).
Yep, perfectly sensible.
To catch the "no suitable C compiler case": the call to setup(...)
will do a sys.exit in case of problems. So, first try it with the ext_modules
argument set as desired, within a try
:
try:
setup(..., ext_modules=...)
except SystemExit: ...
and in the except
clause, call setup(...)
again without the ext_modules
(so it gives up on building and installing the extensions). The user who's installing will still see messages like "unable to execute gcc-4.0: No such file or directory", but you can appropriately add your own messages to inform the user that it's no big deal and that you're trying again without the extension modules.
To support non-CPython implementations, in your setup.py
you can test sys.version
(I'm not sure what the value will be for each non-CPython implementation, but IronPython has an 'IronPython'
substring there, for example), to avoid even trying the ext_modules
part. If you miss some such implementation in your checks, the try/except should probably catch most others anyway, just with a modest amount of wasted work;-).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With