{"id":594,"date":"2014-11-12T15:33:33","date_gmt":"2014-11-12T06:33:33","guid":{"rendered":"https:\/\/blog.ymyzk.com\/?p=594"},"modified":"2014-12-03T09:53:28","modified_gmt":"2014-12-03T00:53:28","slug":"setuptools-cython","status":"publish","type":"post","link":"https:\/\/blog.ymyzk.com\/2014\/11\/setuptools-cython\/","title":{"rendered":"Cython \u3092\u5229\u7528\u3059\u308b\u305f\u3081\u306e setup.py \u306e\u66f8\u304d\u65b9"},"content":{"rendered":"

Cython \u306e\u30b3\u30fc\u30c9\u3092\u62e1\u5f35\u30e2\u30b8\u30e5\u30fc\u30eb\u3068\u3057\u3066\u30d1\u30c3\u30b1\u30fc\u30b8\u306b\u7d44\u307f\u8fbc\u3080\u306b\u306f setup.py \u306b\u5909\u66f4\u3092\u52a0\u3048\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059.<\/p>\n

<\/p>\n

distutils \u3092\u7528\u3044\u308b\u65b9\u6cd5<\/h2>\n

distutils \u3092\u7528\u3044\u308b\u65b9\u6cd5\u306f\u516c\u5f0f\u30c9\u30ad\u30e5\u30e1\u30f3\u30c8 (\u82f1\u8a9e<\/a> \/ \u65e5\u672c\u8a9e<\/a>) \u306b\u63b2\u8f09\u3055\u308c\u3066\u3044\u307e\u3059.<\/p>\n

hello.pyx \u3068\u3044\u3046 Cython \u30b3\u30fc\u30c9\u3092\u4f5c\u6210\u3057, \u4ee5\u4e0b\u306e\u3088\u3046\u306b setup.py \u3092\u8a18\u8ff0\u3059\u308b\u3068\u81ea\u52d5\u7684\u306b Cython \u306b\u3088\u308b C \u30b3\u30fc\u30c9\u3078\u306e\u30b3\u30f3\u30d1\u30a4\u30eb\u3068 C \u30b3\u30fc\u30c9\u306e\u30b3\u30f3\u30d1\u30a4\u30eb\u304c\u884c\u308f\u308c\u307e\u3059.<\/p>\n

\r\nfrom distutils.core import setup\r\nfrom Cython.Build import cythonize\r\n\r\nsetup(\r\n    name = 'Sample App',\r\n    ext_modules = cythonize(\"hello.pyx\"),\r\n)\r\n<\/pre>\n

\u3053\u308c\u306b\u3088\u3063\u3066\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u3055\u308c\u305f\u30e2\u30b8\u30e5\u30fc\u30eb\u306f import hello \u3068\u3059\u308c\u3070 import \u3067\u304d\u307e\u3059.<\/p>\n

setuptools \u3092\u7528\u3044\u308b\u65b9\u6cd5<\/h2>\n

\u73fe\u5728 Python \u306e\u30d1\u30c3\u30b1\u30fc\u30b8\u30f3\u30b0\u306f setuptools \u3067\u884c\u3046\u3053\u3068\u304c\u4e00\u822c\u7684\u306a\u305f\u3081, setuptools \u3092\u7528\u3044\u308b\u65b9\u6cd5\u3092\u7d39\u4ecb\u3057\u307e\u3059.<\/p>\n

\u3053\u3053\u3067\u306f mypackage \u3068\u3044\u3046\u540d\u524d\u3067\u30d1\u30c3\u30b1\u30fc\u30b8\u3092\u4f5c\u6210\u3057, mypackage\/__init__.py \u3068 mypackage\/hello.pyx \u3092\u4f5c\u6210\u3059\u308b\u3053\u3068\u3068\u3057\u307e\u3059. setup.py \u306f\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u306a\u308a\u307e\u3059.<\/p>\n

\r\nfrom Cython.Distutils import build_ext\r\nfrom setuptools import setup, Extension\r\n\r\next_modules = [\r\n    Extension('mypackage.hello', sources=['mypackage\/hello.pyx'])\r\n]\r\n\r\nsetup(\r\n    name='Sample App',\r\n    packages=['mypackage'],\r\n    ext_modules=ext_modules,\r\n    cmdclass={'build_ext': build_ext}\r\n)\r\n<\/pre>\n

\u3053\u308c\u306b\u3088\u3063\u3066\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u3055\u308c\u305f\u30e2\u30b8\u30e5\u30fc\u30eb\u306f from mypackage import hello \u3068\u3059\u308c\u3070 import \u3067\u304d\u307e\u3059.<\/p>\n

C \u30b3\u30f3\u30d1\u30a4\u30e9\u306e\u30aa\u30d7\u30b7\u30e7\u30f3\u306e\u8a2d\u5b9a<\/h2>\n

Cython \u3067\u751f\u6210\u3057\u305f\u751f\u6210\u3057\u305f C \u306e\u30b3\u30fc\u30c9\u3092\u30b3\u30f3\u30d1\u30a4\u30eb\u3059\u308b\u969b\u306b, \u30a4\u30f3\u30af\u30eb\u30fc\u30c9\u30d1\u30b9\u3084\u30b3\u30f3\u30d1\u30a4\u30e9\u30aa\u30d7\u30b7\u30e7\u30f3\u3092\u8a2d\u5b9a\u3057\u305f\u3044\u5834\u5408\u304c\u3042\u308a\u307e\u3059. \u4e0a\u306e setuptools \u306e\u4f8b\u3067, Extension \u306e\u90e8\u5206\u3092\u7de8\u96c6\u3059\u308b\u3053\u3068\u3067\u3053\u308c\u304c\u53ef\u80fd\u306b\u306a\u308a\u307e\u3059. setup.py \u306f\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u306a\u308a\u307e\u3059.<\/p>\n

\r\nfrom Cython.Distutils import build_ext\r\nfrom setuptools import setup, Extension\r\n\r\next_modules = [\r\n    Extension('mypackage.hello',\r\n              sources=['mypackage\/hello.pyx'],\r\n              include_dirs=['DIR_1', 'DIR_2'],\r\n              extra_compile_args=['--option-a', '-O3'])\r\n]\r\n\r\nsetup(\r\n    name='Sample App',\r\n    packages=['mypackage'],\r\n    ext_modules=ext_modules,\r\n    cmdclass={'build_ext': build_ext}\r\n)\r\n<\/pre>\n

\u30a4\u30f3\u30af\u30eb\u30fc\u30c9\u30d1\u30b9\u3084\u30b3\u30f3\u30d1\u30a4\u30e9\u30aa\u30d7\u30b7\u30e7\u30f3\u306f\u30ea\u30b9\u30c8\u3067\u6307\u5b9a\u3059\u308b\u3053\u3068\u306b\u6ce8\u610f\u3057\u3066\u304f\u3060\u3055\u3044. \u307e\u305f, \u30b3\u30f3\u30d1\u30a4\u30e9\u30aa\u30d7\u30b7\u30e7\u30f3\u306f\u30c7\u30d5\u30a9\u30eb\u30c8\u3067\u8a2d\u5b9a\u3055\u308c\u308b\u3082\u306e\u3082\u3042\u308b\u305f\u3081, \u6ce8\u610f\u3057\u3066\u8a2d\u5b9a\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059.<\/p>\n

\u3061\u306a\u307f\u306b Numpy \u306e\u30a4\u30f3\u30af\u30eb\u30fc\u30c9\u30d1\u30b9\u306f numpy.get_include() \u3067\u53d6\u5f97\u3067\u304d\u307e\u3059.<\/p>\n

C \u30b3\u30fc\u30c9\u3068 Cython \u30b3\u30fc\u30c9\u3092\u81ea\u52d5\u7684\u306b\u9078\u629e\u3059\u308b<\/h2>\n

\u30d1\u30c3\u30b1\u30fc\u30b8\u3092\u914d\u5e03\u3059\u308b\u969b\u306b Cython \u306e\u30b3\u30fc\u30c9\u3068 Cython \u3067 C \u306b\u5909\u63db\u3057\u305f\u30b3\u30fc\u30c9\u306e\u4e21\u65b9\u3092\u914d\u5e03\u3059\u308c\u3070, Cython \u3092\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u3057\u3066\u3044\u306a\u3044\u30e6\u30fc\u30b6\u30fc\u3067\u3082\u30d1\u30c3\u30b1\u30fc\u30b8\u3092\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u3067\u304d\u308b\u3088\u3046\u306b\u306a\u308a\u307e\u3059. \u3053\u306e\u305f\u3081 Cython \u306e\u30b3\u30fc\u30c9\u3092\u914d\u5e03\u3059\u308b\u969b\u306f C \u306e\u30b3\u30fc\u30c9\u3082\u3068\u3082\u306b\u914d\u5e03\u3059\u308b\u3088\u3046\u306b\u3057\u305f\u307b\u3046\u304c\u3088\u3055\u305d\u3046\u3067\u3059.<\/p>\n

C \u306e\u30b3\u30fc\u30c9\u3068 Cython \u306e\u30b3\u30fc\u30c9\u3092\u5171\u306b\u914d\u5e03\u3059\u308b\u5834\u5408, setup.py \u3067\u3069\u3061\u3089\u306e\u30b3\u30fc\u30c9\u304b\u3089\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u3059\u308b\u304b\u3092\u9078\u629e\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059. mypackage\/__init__.py \u3068 mypackage\/hello.pyx, mypackage\/hello.c \u3068\u3044\u3046\u30d5\u30a1\u30a4\u30eb\u69cb\u6210\u3068\u3057\u3066\u4f5c\u6210\u3057\u305f setup.py \u306f\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u306a\u308a\u307e\u3059.<\/p>\n

\r\nfrom setuptools import setup, Extension\r\n\r\ntry:\r\n    from Cython.Distutils import build_ext\r\n    USE_CYTHON = True\r\nexcept ImportError:\r\n    USE_CYTHON = False\r\n\r\nif USE_CYTHON:\r\n    ext = '.pyx'\r\n     cmdclass = {'build_ext': build_ext}\r\nelse:\r\n    ext = '.c'\r\n    cmdclass = {}\r\n\r\next_modules = [\r\nExtension(\r\n    'mypackage.hello',\r\n    sources=['mypackage\/hello' + ext])\r\n]\r\n\r\nsetup(\r\n    name='Sample',\r\n    packages=['mypackage'],\r\n    ext_modules=ext_modules,\r\n    cmdclass=cmdclass\r\n)\r\n<\/pre>\n

\u306f\u3058\u3081\u306b try \u6587\u3067 Cython \u3092\u8aad\u307f\u8fbc\u3081\u308b\u304b\u5224\u5b9a\u3057, \u305d\u308c\u306b\u3088\u3063\u3066\u30d5\u30e9\u30b0\u3092\u8a2d\u5b9a\u3057\u3066\u304a\u304d\u307e\u3059. \u305d\u306e\u30d5\u30e9\u30b0\u3092\u3082\u3068\u306b\u62e1\u5f35\u5b50\u7b49\u3092\u8a2d\u5b9a\u3059\u308b\u3088\u3046\u306b\u3057\u3066\u3044\u307e\u3059.<\/p>\n","protected":false},"excerpt":{"rendered":"

Cython \u306e\u30b3\u30fc\u30c9\u3092\u62e1\u5f35\u30e2\u30b8\u30e5\u30fc\u30eb\u3068\u3057\u3066\u30d1\u30c3\u30b1\u30fc\u30b8\u306b\u7d44\u307f\u8fbc\u3080\u306b\u306f setup.py \u306b\u5909\u66f4\u3092\u52a0\u3048\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059.<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[63,15],"_links":{"self":[{"href":"https:\/\/blog.ymyzk.com\/wp-json\/wp\/v2\/posts\/594"}],"collection":[{"href":"https:\/\/blog.ymyzk.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.ymyzk.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.ymyzk.com\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.ymyzk.com\/wp-json\/wp\/v2\/comments?post=594"}],"version-history":[{"count":11,"href":"https:\/\/blog.ymyzk.com\/wp-json\/wp\/v2\/posts\/594\/revisions"}],"predecessor-version":[{"id":624,"href":"https:\/\/blog.ymyzk.com\/wp-json\/wp\/v2\/posts\/594\/revisions\/624"}],"wp:attachment":[{"href":"https:\/\/blog.ymyzk.com\/wp-json\/wp\/v2\/media?parent=594"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.ymyzk.com\/wp-json\/wp\/v2\/categories?post=594"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.ymyzk.com\/wp-json\/wp\/v2\/tags?post=594"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}