How should I structure a Python package that contains Cython code

后端 未结 10 1857
傲寒
傲寒 2020-12-22 15:11

I\'d like to make a Python package containing some Cython code. I\'ve got the the Cython code working nicely. However, now I want to know how best to package it.

For

10条回答
  •  清酒与你
    2020-12-22 15:17

    The easiest is to include both but just use the c-file? Including the .pyx file is nice, but it's not needed once you have the .c file anyway. People who want to recompile the .pyx can install Pyrex and do it manually.

    Otherwise you need to have a custom build_ext command for distutils that builds the C file first. Cython already includes one. http://docs.cython.org/src/userguide/source_files_and_compilation.html

    What that documentation doesn't do is say how to make this conditional, but

    try:
         from Cython.distutils import build_ext
    except ImportError:
         from distutils.command import build_ext
    

    Should handle it.

提交回复
热议问题