Cython Stratup

Cython 簡單上手

1. 使用 pip install 安裝 Cython 套件:

$ pip install Cython

2. 在沒有中文的路徑下建立資料夾 (例如: home)

$ cd ~
$ mkdir MyCythonTest
$ cd MyCythonTest # 切換到該資料夾底下

3. 編輯一個 Cython 檔案,附檔名為".pyx"

$ vim helloworld.pyx

編輯 helloworld.pyx

print("Hello world")

3. 編輯一個 setup.py 檔案,類似 py 的 Makefile

$ vim setup.py 

setup.py:

from distutils.core import setup
from Cython.Build import cythonize
           
setup (ext_modules = cythonize("helloworld.pyx"))








4. Build setup.py,"--inplace" 指明目標是本資料夾下

$ python setup.py build_ext --inplace

5. 進入 python command line

$ python
>>import helloworld
Hello World

留言

熱門文章