2

I have a C code which calls a Fortran subroutine called SetFlags. I would like to turn this C code into a python module. It creates a .so file, but I cannot import this module into python. I am not sure if my error is in the creation of the module using distutils or in linking to the fortran library. Here is my setflagsmodule.c file

#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;
}

Here is my setup file called 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'])],
    )

I build the module using:

python setflags.py build_ext --inplace

When I try to import the module into python this is the result:

>>> import setflags
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: dynamic module does not define init function (initsetflags)

Does anyone have a recommendation on how to solve this ImportError?

Any help would be greatly appreciated and thank you in advance for your time.

4
  • Have you tried just removing all the Fortran references and seeing if you get the same error? That would be the obvious way to rule out your question. Commented Jun 6, 2013 at 21:47
  • Why are bothering with distutils packaging when you don't even have the module running yet? Get it working first, then worry about packaging. Commented Jun 6, 2013 at 22:00
  • @LeeDanielCrocker: A lot of people don't know how to compile C extensions without distutils, because that's the first (and, for POSIX, only) way the tutorial shows you how to do it. See Compilation and Linkage. And even after you learn how to do cc $(python-config --cflags) $(python-config --ldflags) foomodule.c -o foomodule.so, it's often easier to write the setup.py once and be done with it. Commented Jun 7, 2013 at 18:25
  • Interesting. I could never figure out how to get distutils to work for my library. But then, I'm a C programmer first. Commented Jun 7, 2013 at 19:28

1 Answer 1

3

The problem is very simple, but easy to miss.

Notice the error you get:

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

Now look at your code:

PyMODINIT_FUNC init_setflags(void)

You've defined init_setflags instead of initsetflags. Just remove the extra underscore, and it should work.


From the documentation on The Module's Method Table and Initialization Function:

The initialization function must be named initname(), where name is the name of the module…


The reason you often see init functions named init_foo in examples is that they're usually initializing a module _foo.so, which will then be wrapped by a pure-Python module foo.py.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.