当前位置: 代码迷 >> 综合 >> Cython速生成Python扩展模块
  详细解决方案

Cython速生成Python扩展模块

热度:13   发布时间:2023-09-30 21:58:48.0

Cython语法融合C和python

1.pip install Cython

2.helloworld目录下创建helloworld.pyx

内容:

cdef extern from"stdio.h":

    extern int printf(const char *format, ...)

def SayHello():

printf("hello,world\n")

3.同目录建Setup.py

内容:

from distutils.core import setup

from distutils.extension import Extension

from Cython.Build import cythonize

 

setup(

  name = 'helloworld',

  ext_modules=cythonize([

    Extension("helloworld", ["helloworld.pyx"]),

    ]),

)

 4.编译

cmd 输入:

python Setup.py build

5.新建__init()__.py

内容:

from helloworld import *

6.helloworld的前一个目录新建test.py

内容:

import helloworld

helloworld.SayHello()

7.cmd 输入:

python test.py



  相关解决方案