Last active
December 8, 2018 18:32
-
-
Save geoffreygarrett/7b6968fde0d514d9ac43a62f00c77423 to your computer and use it in GitHub Desktop.
[setup.py with cython] #cython #python #python3 #setup #setup.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# REFERENCE: https://stackoverflow.com/questions/4505747/how-should-i-structure-a-python-package-that-contains-cython-code | |
# github) (I don't expect this to be a popular package, but it was a good chance to learn Cython). | |
# This method relies on the fact that building a .pyx file with Cython.Distutils.build_ext (at least with Cython version 0.14) always seems to create a .c file in the same directory as the source .pyx file. | |
# Here is a cut-down version of setup.py which I hope shows the essentials: | |
from distutils.core import setup | |
from distutils.extension import Extension | |
try: | |
from Cython.Distutils import build_ext | |
except ImportError: | |
use_cython = False | |
else: | |
use_cython = True | |
cmdclass = { } | |
ext_modules = [ ] | |
if use_cython: | |
ext_modules += [ | |
Extension("mypackage.mycythonmodule", [ "cython/mycythonmodule.pyx" ]), | |
] | |
cmdclass.update({ 'build_ext': build_ext }) | |
else: | |
ext_modules += [ | |
Extension("mypackage.mycythonmodule", [ "cython/mycythonmodule.c" ]), | |
] | |
setup( | |
name='mypackage', | |
... | |
cmdclass = cmdclass, | |
ext_modules=ext_modules, | |
... | |
) | |
# I also edited MANIFEST.in to ensure that mycythonmodule.c is included in a source distribution (a source distribution that is created with python setup.py sdist): | |
# ... | |
# recursive-include cython * | |
# ... | |
# I don't commit mycythonmodule.c to version control 'trunk' (or 'default' for Mercurial). When I make a release, I need to remember to do a python setup.py build_ext first, to ensure that mycythonmodule.c is present and up-to-date for the source code distribution. I also make a release branch, and commit the C file into the branch. That way I have a historical record of the C file that was distributed with that release. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment