教程

编译单个文件

  1. 制作 setup.py

    1
    2
    3
    4
    5
    6
    
    from setuptools import setup
    from Cython.Build import cythonize
    
    setup(
        ext_modules = cythonize("helloworld.pyx")
    )
    • 这里假设我们要处理的脚本是 helloworld.pyx
  2. 命令行执行

    1
    
    python setup.py build_ext --inplace

直接使用 .pyx 模块

  • 需要添加的代码

    1
    2
    3
    
    >>> import pyximport; pyximport.install()
    >>> import helloworld
    Hello World

编译多个 模块(文件) 的项目

设置多个 Extension

  • 例子

    1
    2
    3
    4
    
    setup(
        cmdclass = {'build_ext': build_ext},
        ext_modules = [Extension("example", sourcefiles), Extension("example2", sourcefiles2), Extension("example3", sourcefiles3)]
    )

梯级 项目结构 PackageHierarchy

因 Cython 编译 pyd 造成 ZeroDivisionError