待编译源文件: math_code.py
def fact(n):
if n==0 or n==1:
return 1
else:
return n*fact(n-1)
def fibo(n):
if n==0 or n==1:
return 1
else:
return fibo(n-1)+fibo(n-2)
def dicho(f, a, b, eps):
assert f(a)*f(b) < 0
while b-a > eps:
M = (a+b)/2.
if f(a) * f(M) > 0:
a = M
else:
b = M
return M
def newton(f, fp, x0, eps):
u = x0
v = u - f(u)/fp(u)
while abs(v-u) > eps:
u = v
v = u - f(u)/fp(u)
return v
创建编译工具脚本 setup.py
from setuptools import setup
from Cython.Build import cythonize
setup(ext_modules=cythonize("math_code.py"))
执行命令
python setup.py build_ext --inplace
编译后生成文件目录
编译后生成一个文件夹 两个文件
其中 math_code.cp37-win_amd64.pyd
就是我们想要的 pyd文件
这个文件名不能随意更改 不然导入该pyd文件时会报错
不过测试后得知可以把 .cp37-win_amd64
给删掉