Unable to import Python module written in C -


i have been trying work out how make .pyd (python extension module) file c script (without swig or else except mingw) , have built .pyd.

the problem occurs when try , import module.

if run module runs (as far can see) , error appears saying python has stopped working , closes without executing rest of program.

here 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 python extension') 

i compiled script with:

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

i can't find errors when compiling in command prompt , using python 3.6 (running on windows 10).

i can't find on subject , prefer keep away cython (i know c) , swig.

any tell me wrong fantastic.

creating python extension different writing regular c code. have done creating valid c program doesn't make sense python.

that's how program should (it's skeleton, not 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] python interpreter */     py_setprogramname(argv[0]);      /* initialize python interpreter. required. */     py_initialize();      /* add static module */     init_test_methods(); } 

i recommend read more @ following link: http://dan.iel.fm/posts/python-c-extensions/ in official docs.


Comments

Popular posts from this blog

python Tkinter Capturing keyboard events save as one single string -

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

javascript - Z-index in d3.js -