2016-08-18 6 views
2

イムを定義していません(のpython3ではをsetup.py installを)が、PythonははImportErrorを上げる:動的モジュールは、モジュールのエクスポート機能(PyInit_costFunctionを定義していません。 ) エラー!のpython:はImportError:動的モジュールはCで書かれた私の機能をインストールしようとしているモジュールのエクスポート機能に

costFunction.c:

static PyObject *costFunction(PyObject *self, PyObject *args) 
{ 
    return Py_BuildValue("d", 0); // or anything! 
} 

static PyMethodDef costFunction_methods[] = { 
    {"costFunction", (PyCFunction)costFunction, METH_VARARGS, "cost function"}, 
    {NULL, NULL, 0, NULL} 
}; 

static struct PyModuleDef costFunctionmodule = { 
    PyModuleDef_HEAD_INIT,"costFunction", NULL, -1, costFunction_methods 
}; 

PyMODINIT_FUNC PyInit_costFunction(void) 
{ 
    return PyModule_Create(&costFunctionmodule); 
} 

setup.py:

from distutils.core import setup, Extension 
setup(name='costFunction', version='1.0', \ 
     ext_modules=[Extension('costFunction', ['costFunction.c'],include_dirs=['include'])]) 

外部ライブラリ:

tinyexpr私は、PythonでLinuxのミント18を使用しています3.5.2

EDIT: のpython3-devのバージョンは、最後に、私は汚いトリックを3.5.1-3

答えて

1

使用されています!

と(はPython.h及びCにおける任意のPythonデータ型なし)

コンパイルされたCコード:ロードし、それを使用する

gcc -fPIC -Wall -O3 costFunction.c -o costFunction.so -shared -fopenmp 

及び使用のPythonのctypesモジュール:

dll = ctypes.CDLL("./costFunction.so") 
costFunction = dll.cost_function 
costFunction.restype = ctypes.c_double 
costFunction.argtypes = [ctypes.POINTER(ctypes.c_double), ctypes.c_int] 
関連する問題