C API from Extension Module in Python | Set 2 Last Updated : 27 Mar, 2019 Summarize Comments Improve Suggest changes Share Like Article Like Report Prerequisite: C API from Extension Module in Python | Set 1 Let's see an example of a new extension module that loads and uses these API functions that we build up in the previous article. Code #1 : C #include "pythonsample.h" /* An extension function that uses the exported API */ static PyObject *print_point(PyObject *self, PyObject *args) { PyObject *obj; Point *p; if (!PyArg_ParseTuple(args, "O", &obj)) { return NULL; } /* Note: This is defined in a different module */ p = PyPoint_AsPoint(obj); if (!p) { return NULL; } printf("%f %f\n", p->x, p->y); return Py_BuildValue(""); } static PyMethodDef PtExampleMethods[] = { {"print_point", print_point, METH_VARARGS, "output a point"}, { NULL, NULL, 0, NULL} }; static struct PyModuleDef ptexamplemodule = { PyModuleDef_HEAD_INIT, /* name of module */ "ptexample", /* Doc string (may be NULL) */ "A module that imports an API", /* Size of per-interpreter state or -1 */ -1, /* Method table */ PtExampleMethods }; Code #2 : Module initialization function C PyMODINIT_FUNC PyInit_ptexample(void) { PyObject *m; m = PyModule_Create(&ptexamplemodule); if (m == NULL) return NULL; /* Import sample, loading its API functions */ if (!import_sample()) { return NULL; } return m; } Now to compile this new module, one need not bother about how to link against any of the libraries or code from the other module. One can just simply use work.py file as shown below. Code #3 : Python3 1== # setup.py from distutils.core import setup, Extension # May need pythonsample.h directory setup(name ='ptexample', ext_modules = [ Extension('ptexample', ['ptexample.c'], include_dirs = [], )]) After performing all this task, this new extension function works perfectly with the C API functions defined in the other module. Code #4 : Using CPI API functions defined in the other module Python3 1== import ptexample import work point1 = work.Point(2, 3) print ("Point_1 : ", point1) print ("\n", ptexample.print_point(p1)) Output : Point_1 : 2.000000 3.000000 Comment More infoAdvertise with us Next Article C Extension Module using Python M manikachandna97 Follow Improve Article Tags : Python Python-ctype Practice Tags : python Similar Reads C API from Extension Module in Python | Set 1 Suppose given a C extension module that internally defines a variety of useful functions that can be exported as a public C API for use elsewhere. Now if we want to use these functions inside other extension modules. Then, it is important to know how to link them together but doing it with the C com 3 min read C Extension Module using Python Writing a simple C extension module directly using Pythonâs extension API and no other tools. It is straightforward to make a handcrafted extension module for a simple C code. But first, we have to make sure that the C code has a proper header file. Code #1 : C #include <math.h> extern int gcd 4 min read How to create modules in Python 3 ? Modules are simply python code having functions, classes, variables. Any python file with .py extension can be referenced as a module. Although there are some modules available through the python standard library which are installed through python installation, Other modules can be installed using t 4 min read Create and Import modules in Python In Python, a module is a self-contained Python file that contains Python statements and definitions, like a file named GFG.py, can be considered as a module named GFG which can be imported with the help of import statement. However, one might get confused about the difference between modules and pac 3 min read Introduction to Python Typing-Extensions Module The typing-extensions module provides backports of the latest typing features to ensure that developers working with older versions of Python can still leverage these advanced tools. This module acts as a bridge between future releases of Python and existing codebases, enabling us to stay up to date 8 min read Python | Passing Filenames to Extension in C Filename has to be encoded according to the systemâs expected filename encoding before passing filenames to C library functions. Code #1 : To write an extension function that receives a filename CPP static PyObject* py_get_filename(PyObject* self, PyObject* args) { PyObject* bytes; char* filename; P 2 min read Like