Cython Notes
文章目录
教程
官方 Tutorial
编译单个文件
制作 setup.py
1 2 3 4 5 6from setuptools import setup from Cython.Build import cythonize setup( ext_modules = cythonize("helloworld.pyx") )- 这里假设我们要处理的脚本是 helloworld.pyx
命令行执行
1python setup.py build_ext --inplace
直接使用 .pyx 模块
需要添加的代码
1 2 3>>> import pyximport; pyximport.install() >>> import helloworld Hello World
编译多个 模块(文件) 的项目
设置多个 Extension
例子
1 2 3 4setup( cmdclass = {'build_ext': build_ext}, ext_modules = [Extension("example", sourcefiles), Extension("example2", sourcefiles2), Extension("example3", sourcefiles3)] )
梯级 项目结构 PackageHierarchy
cython 官方 github
因 Cython 编译 pyd 造成 ZeroDivisionError
参考
因为 cython 把 数 转换成 c/c++ 类型
导致很小的数,识别成 0
1result = - self.get_deltaG(temp, extrapolate) / (10**-3 * CONSTANT_R * temp)解说
这里 10**-3 是一个很小的数
- 在转换成 c/c++, 可能被认为是 对整型操作
- 取整,保持了零
正确做法
- 把 10**-3 —> 换成 10.0**-3
浮点数操作
1result = - self.get_deltaG(temp, extrapolate) / (10.0**-3 * CONSTANT_R * temp)
文章作者
上次更新 2022-03-24 (d2bfef1)