У меня есть код C, который вызывает подпрограмму Fortran, называемую SetFlags. Я хотел бы превратить этот код C в модуль Python. Он создает файл .so, но я не могу импортировать этот модуль в python. Я не уверен, что моя ошибка связана с созданием модуля с помощью distutils или с подключением к библиотеке fortran. Вот мой файл setflagsmodule.c
#include <Python/Python.h>
#include "/Users/person/program/x86_64-Darwin/include/Cheader.h"
#include <stdlib.h>
#include <stdio.h>
static char module_docstring[] =
"This module provides an interface for Setting Flags in C";
static char setflags_docstring[] =
"Set the Flags for program";
static PyObject * setflags(PyObject *self, PyObject *args)
{
int *error;
const int mssmpart;
const int fieldren;
const int tanbren;
const int higgsmix;
const int p2approx;
const int looplevel;
const int runningMT;
const int botResum;
const int tlcplxApprox;
if (!PyArg_ParseTuple(args, "iiiiiiiiii", &error,&mssmpart,&fieldren,&tanbren,&higgsmix,&p2approx,&looplevel,&runningMT,&botResum,&tlcplxApprox))
return NULL;
FSetFlags(error,mssmpart,fieldren,tanbren,higgsmix,p2approx,looplevel,runningMT,botResum,tlcplxApprox); //Call fortran subroutine
return Py_None;
}
static PyMethodDef setflags_method[] = {
{"FSetFlags", setflags, METH_VARARGS, setflags_docstring},
{NULL,NULL,0,NULL}
};
PyMODINIT_FUNC init_setflags(void)
{
PyObject *m;
m = Py_InitModule3("setflags", setflags_method, module_docstring);
if (m == NULL)
return;
}
Вот мой установочный файл с именем setflags.py:
from distutils.core import setup, Extension
setup(
ext_modules=[Extension("setflags",["setflagsmodule.c"], include_dirs=['/Users/person/program/x86_64-Darwin'],
library_dirs=['/Users/person/program/x86_64-Darwin/lib/'], libraries=['FH'])],
)
Я строю модуль, используя:
python setflags.py build_ext --inplace
Когда я пытаюсь импортировать модуль в python, это результат:
>>> import setflags
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: dynamic module does not define init function (initsetflags)
У кого-нибудь есть рекомендации о том, как решить эту ошибку ImportError?
Мы будем очень признательны за любую помощь и заранее благодарим вас за ваше время.
cc $(python-config --cflags) $(python-config --ldflags) foomodule.c -o foomodule.so
, зачастую проще написатьsetup.py
один раз и покончить с этим. - person abarnert   schedule 07.06.2013