How should I structure a Python package that contains Cython code

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

    Adding to Craig McQueen's answer: see below for how to override the sdist command to have Cython automatically compile your source files before creating a source distribution.

    That way your run no risk of accidentally distributing outdated C sources. It also helps in the case where you have limited control over the distribution process e.g. when automatically creating distributions from continuous integration etc.

    from distutils.command.sdist import sdist as _sdist
    
    ...
    
    class sdist(_sdist):
        def run(self):
            # Make sure the compiled Cython files in the distribution are up-to-date
            from Cython.Build import cythonize
            cythonize(['cython/mycythonmodule.pyx'])
            _sdist.run(self)
    cmdclass['sdist'] = sdist
    

提交回复
热议问题