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.

Xantium
  • 11,201
  • 10
  • 62
  • 89
  • 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_**](https://docs.python.org/3/extending/building.html?highlight=pythonpath) in the documentation. – martineau Aug 14 '17 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? – user2357112 Aug 14 '17 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**' – Xantium Aug 14 '17 at 19:35

1 Answers1

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.

Matheus Portela
  • 2,420
  • 1
  • 21
  • 32
  • 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)? – Xantium Aug 14 '17 at 19:45
  • 1
    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: https://github.com/matheusportela/caesar-cipher – Matheus Portela Aug 14 '17 at 23:07
  • 1
    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 ¦ ] – Xantium Aug 15 '17 at 18:21
  • 1
    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](https://stackoverflow.com/questions/34135280/valueerror-unknown-ms-compiler-version-1900/43971456#43971456) and it finally compiled. – Xantium Aug 17 '17 at 00:34
  • Great, @Simon! Since I don't usually program in Windows, I wasn't aware of that problem. Good to know you figured it out. – Matheus Portela Aug 17 '17 at 15:48