0

I have been trying to work out how to make a .pyd (Python Extension Module) file from a C script (without swig or anything else except MinGW) and have successfully built it into a .pyd.

The problem however occurs when I try and import the module.

If I run it the module runs successfully (as far as I can see) and then an error appears saying Python Has Stopped Working and it closes without executing the rest of the program.

Here is my C script (test.c):

#include <python.h>

int main()
{
    PyInit_test();
    return 0;
}

int PyInit_test()
{
    printf("hello world");
}

And Python Script (file.py):

import test
print('Run From Python Extension')

I compiled the script with:

gcc -c file.py
gcc -shared -o test.pyd test.c

I can't find any errors when compiling in command prompt and am using python 3.6 (running on Windows 10).

I can't find much on the subject and would prefer to keep away from Cython (I already know C) and Swig.

Any help to tell me what is wrong would be fantastic.

3
  • Is the .pyd file in the same folder as the Python script that tries to import it? Python searches for modules in a defined way in certain locations, and it may be simply that your library isn't in any of the places that it looks. See Building C and C++ Extensions in the documentation. Commented Aug 14, 2017 at 18:23
  • 1
    Your module initialization function doesn't make the slightest attempt to initialize a module. It doesn't even have the right return type. Did you read the docs at all? Commented Aug 14, 2017 at 18:31
  • @martineau I placed the .pyd in the same folder as the python script I tried to import it from. That got me thinking and I put it in Lib. From here I was able to import it without getting the error however 'hello world' did not print just 'finished' Commented Aug 14, 2017 at 19:35

1 Answer 1

4

Creating a Python extension is completely different than writing regular C code. What you have done is simply creating a valid C program but that doesn't make sense for Python.

That's how your program should look like (it's just a skeleton, not the proper, working code):

#include <Python.h>
#include <stdlib.h>

static PyObject* test(PyObject* self, PyObject* args)
{
    printf("hello world");
    return NULL;
}

static PyMethodDef test_methods[] = {
    {"test", test, METH_VARARGS, "My test method."},
    {NULL, NULL, 0, NULL} /* Sentinel */
};

PyMODINIT_FUNC init_test_methods() {
    Py_InitModule("test", test_methods);
}

int main(int argc, char** argv)
{
    /* Pass argv[0] to the Python interpreter */
    Py_SetProgramName(argv[0]);

    /* Initialize the Python interpreter. Required. */
    Py_Initialize();

    /* Add a static module */
    init_test_methods();
}

I recommend you read more about this at the following link: http://dan.iel.fm/posts/python-c-extensions/ as well as in the official docs.

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

5 Comments

That makes a lot of sense. Thanks for the help. As you say it is different, how about the compilation is that all right or do I have to do something different to compile it (the setup script)?
There is no secret about the compilation. If you wish, take a look at this project of mine, in which I implemented the Caesar Cipher in C and provided the Python extension to it: github.com/matheusportela/caesar-cipher
That's sorted the compilation problem and gives me a fantastic example to study. As for the compilation it's setup.py build not what I said previously. Thanks again ¦ ]
Just a final note: setup.py build keeps generating an error saying unable to find vcvarsall.bat I used -c mingw32 flag on the end. This gave another error Unknown MS Compiler version 1900 which was solved here and it finally compiled.
Great, @Simon! Since I don't usually program in Windows, I wasn't aware of that problem. Good to know you figured it out.

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.