URL: http://docscythonja.zouri.jp/index.html
・インストール
sudo easy_install cython
まず適当に「hello_test」などフォルダ作っておく、
・関数定義
hello.pyx(hello_test内に配置)
def say_hello_to(name): print("Hello %s!" % name)
・セットアップファイル
setup.py(hello_test内に配置)
from distutils.core import setup from distutils.extension import Extension from Cython.Distutils import build_ext ext_modules = [Extension("hello", ["hello.pyx"])] setup( name = 'Hello world app', cmdclass = {'build_ext': build_ext}, ext_modules = ext_modules )
・コンパイルとインストール
python setup.py build_ext --inplace
ちなみに「--inplace」オプション
URL: http://docs.python.jp/2.6/distutils/configfile.html
コンパイル済みの拡張モジュールファイル (例えば Unix では ext.so) を pure Python モジュール pkg.mod1 および pkg.mod2 と同じソースディレクトリに置きたいとします。
どういうことかな?
say_hello_to関数が追加されたので。
・test.py
from hello import say_hello_to say_hello_to('shinriyo')
とやれば、
Hello shinriyo!
が実行される。