官方文档指引 https://cython.readthedocs.io/en/latest/src/quickstart/install.html
如果提示 error: Unable to find vcvarsall.bat
安装 Microsoft Visual C/C++ (MSVC)
https://visualstudio.microsoft.com/zh-hans/vs/older-downloads/
安装时尽量最小化选择要安装的内容以减少安装时间
如果提示:fatal error C1083: 无法打开包括文件: “io.h”: No such file or directory,则还需要安装 Windows SDK
https://developer.microsoft.com/zh-cn/windows/downloads/sdk-archive/
最后安装Cython
pip3 install Cython
基本使用
hello.py文件,内容如下:
print("Hello World.")
新建setup.py脚本
from distutils.core import setup from Cython.Build import cythonize setup( ext_modules=cythonize("hello.pyx") )
编译
python setup.py build
编译成功,则在目录下会出现两个文件:hello.c,hello.pyd(若在Linux平台下会出现hello.so),此时.so文件或者.pyd文件就可以像普通的python文件一样,被import
如果需要编译整个目录参考setup.py如下:
from distutils.core import setup
from Cython.Build import cythonize
setup(
script_args=['build', '--build-base', './'],
ext_modules=cythonize("controllers/*.py")
)