Cython
requirements
- windows 10
- Python==3.10.13
- Cython==3.0.8
- gcc==8.1.0
操作步骤
-
新建.pyx文件, 此文件写需要转成C语言链接库的类或函数.
class Cython: @staticmethod def run(): print("Cython run")
-
设置setup.py文件, 固定模板, 'mycython.pyx'为pyx文件名, 改成自己的.
from distutils.core import setup, Extension from Cython.Build import cythonize setup( ext_modules = cythonize(Extension( 'mycython', sources=['mycython.pyx'], language='c', include_dirs=[], library_dirs=[], libraries=[], extra_compile_args=[], extra_link_args=[], )) )
-
通过setup.py生成pyd文件, 在终端输入如下命令:
python setup.py build_ext --inplace --compiler=mingw32 -DMS_WIN64
-
运行后会生成pyd文件, 编写测试文件run.py进行测试.
import mycython if __name__ == '__main__': cython = mycython.Cython() cython.run()
操作要点
- 需安装MinGW并配置环境变量
- 命令行需添加--compiler=mingw32 和 -DMS_WIN64
如 python setup.py build_ext --inplace --compiler=mingw32 -DMS_WIN64