Festina lente

急がば回れ(ラテン語)。時には寄り道も。

cythonで"hello world"

こんな訳で、cythonでHello Worldしてみた。自宅にあるMacだとC-compilerを別で入れる必要が無いのは楽ちんですねー。*1

ちなみに自前環境ではAnacondaていうNumpyとかとかが入っているものにcython自体も入っておりインストール不要でした。
以下、自分用のメモってことで。用意するのは実行したいファイル(hello.pyx)と、コンパイル用のファイル(setup.py)の2つ。

  • hello.pyx
def hello():
    print "Hello World!!"
  • setup.py
try:
    from setuptools import setup
    from setuptools import Extension
except ImportError:
    from distutils.core import setup
    from distutils.extension import Extension
from Cython.Distutils import build_ext
hello_modules = [Extension("hello", ["hello.pyx"])]
setup(
        name = 'C extention module example',
        cmdclass = {'build_hello': build_ext},
        ext_modules = hello_modules
      )


でもって、Terminalでコンパイルしようとしてみる:

$ python setup.py build_ext --inplace
running build_ext
cythoning hello.pyx to hello.c
building 'hello' extension
creating build
creating build/temp.macosx-10.5-x86_64-2.7
gcc -fno-strict-aliasing -I/Users/kyoro1/anaconda/include -arch x86_64 -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -I/Users/kyoro1/anaconda/include/python2.7 -c hello.c -o build/temp.macosx-10.5-x86_64-2.7/hello.o


Agreeing to the Xcode/iOS license requires admin privileges, please re-run as root via sudo.


error: command 'gcc' failed with exit status 69

はい、エラー出るねー。調べてみたら、XCodeのlicenseに引っ掛かってましたとさ、、、stackoverflow.com
ということで、

sudo xcrun cc
pip install junos-eznc

で、licenseを通してから再度buildに挑戦!*2

$ python setup.py build_ext --inplace
running build_ext
skipping 'hello.c' Cython extension (up-to-date)
building 'hello' extension
gcc -fno-strict-aliasing -I/Users/kyoro1/anaconda/include -arch x86_64 -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -I/Users/kyoro1/anaconda/include/python2.7 -c hello.c -o build/temp.macosx-10.5-x86_64-2.7/hello.o
hello.c:1423:28: warning: unused function '__Pyx_PyObject_AsString'
      [-Wunused-function]
static CYTHON_INLINE char* __Pyx_PyObject_AsString(PyObject* o) {
                           ^
hello.c:1420:32: warning: unused function '__Pyx_PyUnicode_FromString'
      [-Wunused-function]
static CYTHON_INLINE PyObject* __Pyx_PyUnicode_FromString(const char* c_str) {
                               ^
hello.c:322:29: warning: unused function '__Pyx_Py_UNICODE_strlen'
      [-Wunused-function]
static CYTHON_INLINE size_t __Pyx_Py_UNICODE_strlen(const Py_UNICODE *u)
                            ^
hello.c:1485:26: warning: unused function '__Pyx_PyObject_IsTrue'
      [-Wunused-function]
static CYTHON_INLINE int __Pyx_PyObject_IsTrue(PyObject* x) {
                         ^
hello.c:1535:33: warning: unused function '__Pyx_PyIndex_AsSsize_t'
      [-Wunused-function]
static CYTHON_INLINE Py_ssize_t __Pyx_PyIndex_AsSsize_t(PyObject* b) {
                                ^
hello.c:1560:33: warning: unused function '__Pyx_PyInt_FromSize_t'
      [-Wunused-function]
static CYTHON_INLINE PyObject * __Pyx_PyInt_FromSize_t(size_t ival) {
                                ^
hello.c:1118:32: warning: unused function '__Pyx_PyInt_From_long'
      [-Wunused-function]
static CYTHON_INLINE PyObject* __Pyx_PyInt_From_long(long value) {
                               ^
hello.c:1165:27: warning: function '__Pyx_PyInt_As_long' is not needed and will
      not be emitted [-Wunneeded-internal-declaration]
static CYTHON_INLINE long __Pyx_PyInt_As_long(PyObject *x) {
                          ^
hello.c:1270:26: warning: function '__Pyx_PyInt_As_int' is not needed and will
      not be emitted [-Wunneeded-internal-declaration]
static CYTHON_INLINE int __Pyx_PyInt_As_int(PyObject *x) {
                         ^
9 warnings generated.
creating build/lib.macosx-10.5-x86_64-2.7
gcc -bundle -undefined dynamic_lookup -L/Users/kyoro1/anaconda/lib -arch x86_64 -arch x86_64 build/temp.macosx-10.5-x86_64-2.7/hello.o -L/Users/kyoro1/anaconda/lib -o build/lib.macosx-10.5-x86_64-2.7/hello.so
copying build/lib.macosx-10.5-x86_64-2.7/hello.so -> 

で、お目当てのものを実行でござる。

$ python
Python 2.7.10 |Anaconda 2.3.0 (x86_64)| (default, May 28 2015, 17:04:42) 
[GCC 4.2.1 (Apple Inc. build 5577)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
Anaconda is brought to you by Continuum Analytics.
Please check out: http://continuum.io/thanks and https://binstar.org
>>> import hello
>>> hello.hello()
Hello World!!

てな具合で、"Hello World!!"できました、とさ♪

  • 参考URL

github.com
github.com

Cython ―Cとの融合によるPythonの高速化

Cython ―Cとの融合によるPythonの高速化

*1:会社PCはWindowsだったので、何かしら設定しないとダメでした、、、

*2:挑戦、てほど難しいことやってないですが、、、