Write a C extension for Python
This is a memo for writing C extensions for python.
Sometimes we need to use python but have some tools written in C/C++ at hand already.
In my recent projects, I want to convert coordinates among several systems like wgs84, gcj02 and bd09ll.
I write my service in python but we only have conversion function in C.
I found two good reference:
- A step by step tutorial here. http://dan.iel.fm/posts/python-c-extensions/
- The python official document on writing extension. https://docs.python.org/2/extending/extending.html
My final work is published on github.
Here are a few things to note:
- Python2 and Python3 are different
- In some linux distributions like CentOS 6, python-devel package is needed for the included
Python.h
file, which can be installed by yum. - Python function supports naming parameters, which needs some modification on the call of
PyArg_ParseTuple
function
This work is licensed under a Creative Commons Attribution 4.0 International License.