2

SetFlags という Fortran サブルーチンを呼び出す C コードがあります。この 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 を解決する方法に関する推奨事項はありますか?

どんな助けでも大歓迎です。お時間をいただきありがとうございます。

4

1 に答える 1

3

問題は非常に単純ですが、見落としがちです。

次のエラーに注意してください。

ImportError: dynamic module does not define init function (initsetflags)

コードを見てください:

PyMODINIT_FUNC init_setflags(void)

init_setflagsの代わりに定義しましinitsetflagsた。余分なアンダースコアを削除するだけで機能します。


The Module's Method Table and Initialization Function のドキュメントから:

初期化関数には という名前を付ける必要がありますinitname()。ここnameで、 はモジュールの名前です…</p>


init例で名前が付けられた関数をよく目にする理由init_fooは、それらが通常モジュールを初期化しており_foo.so、それが純粋な Python モジュールによってラップされるためですfoo.py

于 2013-06-06T21:56:05.660 に答える