Category: Python

How to compile Python code to native executable

First, you need to create a python application, in this case just a “Hello World” test.py file:

$ cat test.py
print "Hello World"

Convert the Python code in C code using Cython:

$ cython --embed test.py

We need to use “–embed” because we don’t want a shared library, but a real main program.

Compile the generated “test.c” file:

$ gcc test.c -o test `pkg-config --libs --cflags python2`

Test it:

$ ./test 
Hello World

Sources:
http://stackoverflow.com/questions/5105482/compile-main-python-program-using-cython
https://bbs.archlinux.org/viewtopic.php?id=180538
http://www.behnel.de/cython200910/talk.html