2

Cython を使用して Python コードを効率的にコンパイルし、高速化する方法を学んでいます。

これが私がこれまでに行ったことです:

  1. という Python ファイルを作成し、そのmath_code_python.py中に 4 つの単純な関数を入れました。
  2. そのファイルを として保存しましたmath_code_cython.pyx
  3. というセットアップファイルを作成しましたsetup.py
  4. を入力python C:\Users\loic\Documents\math_code\setup.py build_ext --inplaceしましたCommand Prompt
  5. math_code_cython.cp36-win_amd64.pyd名前を変更したというコンパイル済みファイルを取得しましたmath_code_pyd.pyd
  6. 最後に、その中test_math_code.pydだけimport math_code_pydで呼び出される Python ファイルを作成しました。このファイルを実行すると、次のメッセージが表示されました。

    ImportError: dynamic module does not define module export function
    

私はいくつかの調査を行いましたが、これらの投稿のおかげで、次のものを提供する必要があることがわかりましたinit function

私の質問は次のとおりです。どうすればいいですか?次のようなものの最後に関数を配置する必要がありmath_code_python.pyますか?

def __init__(self):
    # something ?

Python の私のバージョン:

Python 3.6.1 |Anaconda 4.4.0 (64-bit)| (default, May 11 2017, 13:25:24) [MSC v.1900 64 bit (AMD64)]

math_code_python.py

def fact(n):
    if n==0 or n==1:
        return 1
    else:
        return n*fact(n-1)

def fibo(n):
    if n==0 or n==1:
        return 1
    else:
        return fibo(n-1)+fibo(n-2)

def dicho(f, a, b, eps):
    assert f(a)*f(b) < 0
    while b-a > eps:
        M = (a+b)/2.
        if f(a) * f(M) > 0:
            a = M
        else:
            b = M
    return M

def newton(f, fp, x0, eps):
    u = x0
    v = u - f(u)/fp(u)
    while abs(v-u) > eps:
        u = v
        v = u - f(u)/fp(u)
    return v

setup.py

try:
    from setuptools import setup
except ImportError:
    from distutils.core import setup

from Cython.Distutils import build_ext
from Cython.Build import cythonize

import numpy as np

setup(name = "maido",
      include_dirs = [np.get_include()],
      cmdclass = {'build_ext': build_ext},
      ext_modules = cythonize(r"C:\Users\loic\Documents\math_code\math_code_cython.pyx"),
      )
4

1 に答える 1