2016-08-18 5 views
2

im Versuch, meine Funktion zu installieren, geschrieben in c definieren (mit python3 setup.py installieren), aber Python erhöht Import: dynamisches Modul nicht Modul Exportfunktion (PyInit_costFunction definieren) Fehler!Python: Import: dynamisches Modul nicht Modul Exportfunktion

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'])]) 

externe Bibliothek: tinyexpr

ich verwende Linux Mint 18 mit Python 3.5.2

EDIT: python3-dev Version 3.5.1-3

Antwort

1

schließlich benutzte ich einen schmutzigen Trick!

kompilierte C-Code (ohne python.h und jeden Datentyp in Python C) mit:

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

und Python ctypes Modul verwendet, und es zu laden zu verwenden:

dll = ctypes.CDLL("./costFunction.so") 
costFunction = dll.cost_function 
costFunction.restype = ctypes.c_double 
costFunction.argtypes = [ctypes.POINTER(ctypes.c_double), ctypes.c_int] 
Verwandte Themen