需要使用的庫
pip install cython
創建測試代碼
- hanota.pyx(注意文件的后綴名為“pyx”)
def move(n,a,b,c):
if n==1:
print (a,'-->',c)
else:
move(n-1,a,c,b)
move(1,a,b,c)
move (n-1,b,a,c)
- setup.py
from setuptools import setup
from Cython.Build import cythonize
setup(ext_modules=cythonize('./*.pyx'), language='3')
編譯(cmd)
# 查看幫助
# python setup.py --help
python setup.py build_ext --inplace
編譯結果
創建測試文件
test_pyd.py
from hanota import move
move(3, 'A', 'B', 'C')
將源文件修改為“.pyx”的原因
import只會從后綴為.py/.pyc/.pyo/.so/.pyd的文件中導入模塊,不會進入.pyx文件中尋找