DEV Community


Posted on

Using Python and C together

Python and C, two of the most popular and greatest languages that have come out of programming. Many languages can communicate with each other very easily, but with Python and C it's a little tricky. But first lets start of with why we would want this?

There's no doubt about it that both of these languages are powerful, and incredibly useful. And it pays off sometimes to have the raw performance of the C language being used in a Python project, it can certainly help with procedures such as reducing response and processing times.

What we'll need

Python, and C. That's it.

The code

In this example I'll be using a simple Fibonacci function to demonstrate it all.

#include <Python.h>

// create the function like you normally would in C
int CFib(int n){
    if(n < 2)
        return n;
    else return CFib(n - 1) + CFib(n - 2)
// this function will be binding our python version and our C version together
// will only take one and only one non-keyword arguement
static PyObject* fib(PyObject* self, PyObject* args) {
    int n;
    if(!PyArg_ParseTuple(args, "i", &n))
        return NULL;
     return Py_BuildValue("i", CFib(n))
Enter fullscreen mode Exit fullscreen mode

In the code we can see that we require the Python.h header file, this contains all the relevant methods, functions, attributes etc we need to allow the two languages to work together. We first start off by creating the function normally in C, then using the methods from the Python header file we create it again, but with a few more arguments.

As you can see we're using builders and parsers inside the function. These communicate between both languages to create the python versions of the function(s) in C.

Additionally we need a small setup script written in Python

from distutils.core import setup, Extension

setup(name='ModuleName', version='1.0', ext_modules=[Extension('ModuleName', ['Fib.c'])])
Enter fullscreen mode Exit fullscreen mode

most of it speaks for itself here. We're importing from a library that is built into Python 3+ that allows us to run setup and extension scripts, these both are compatible with C and the Python header file

In order to run this and save it as our own project we need to run these two commands
python build
python install
this will then allow you to call your module from any other python project.

And now for the grand finale

import ModuleName # really should've chosen a better name
Enter fullscreen mode Exit fullscreen mode

prints out 1

This is available on my GitHub if you wish to fork it
Click me!
In addition this is a very basic version, you can follow up on how to go more indepth over at The Python tutorial site

Any errors or suggestions feel free to let me know <3

Top comments (2)

tux0r profile image

Any errors or suggestions feel free to let me know <3

Get rid of the Python part. ;-)

haiman profile image
Alexander • Edited

Thank you for this post.
[FYI: Ubuntu 20.04, Python 3.8] I tried it out just for fun but encountered some problems. I get a "permission denied" back when I run the install:

python install
running install
running build
running build_ext
running install_lib
copying build/lib.linux-x86_64-3.8/ -> /usr/local/lib/python3.8/dist-packages
error: could not create '/usr/local/lib/python3.8/dist-packages/': Permission denied

I solved it by running : sudo chown -R $USER /usr/local/lib/python3.8

However, when trying to run the, it tells me "python not found". I tried running python3 build and install just in case but nope.