Cython

requirements

  1. windows 10
  2. Python==3.10.13
  3. Cython==3.0.8
  4. gcc==8.1.0

操作步骤

  1. 新建.pyx文件, 此文件写需要转成C语言链接库的类或函数.

        class Cython:
           @staticmethod
           def run():
              print("Cython run")
    

  1. 设置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=[],
        ))
        )
    

  1. 通过setup.py生成pyd文件, 在终端输入如下命令:

        python setup.py build_ext --inplace --compiler=mingw32 -DMS_WIN64
    

  1. 运行后会生成pyd文件, 编写测试文件run.py进行测试.

        import mycython
    
    
        if __name__ == '__main__':
           cython = mycython.Cython()
           cython.run()
    



操作要点

  1. 需安装MinGW并配置环境变量
  2. 命令行需添加--compiler=mingw32-DMS_WIN64
    python setup.py build_ext --inplace --compiler=mingw32 -DMS_WIN64
posted on 2024-02-18 15:49  坚持_学习  阅读(18)  评论(0)    收藏  举报