How should I structure a Python package that contains Cython code

后端 未结 10 1844
傲寒
傲寒 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:25

    This is a setup script I wrote which makes it easier to include nested directories inside the build. One needs to run it from folder within a package.

    Givig structure like this:

    __init__.py
    setup.py
    test.py
    subdir/
          __init__.py
          anothertest.py
    

    setup.py

    from setuptools import setup, Extension
    from Cython.Distutils import build_ext
    # from os import path
    ext_names = (
        'test',
        'subdir.anothertest',       
    ) 
    
    cmdclass = {'build_ext': build_ext}
    # for modules in main dir      
    ext_modules = [
        Extension(
            ext,
            [ext + ".py"],            
        ) 
        for ext in ext_names if ext.find('.') < 0] 
    # for modules in subdir ONLY ONE LEVEL DOWN!! 
    # modify it if you need more !!!
    ext_modules += [
        Extension(
            ext,
            ["/".join(ext.split('.')) + ".py"],     
        )
        for ext in ext_names if ext.find('.') > 0]
    
    setup(
        name='name',
        ext_modules=ext_modules,
        cmdclass=cmdclass,
        packages=["base", "base.subdir"],
    )
    #  Build --------------------------
    #  python setup.py build_ext --inplace
    

    Happy compiling ;)

提交回复
热议问题