Home arrow Journal arrow Create binary app from python source code using Cython
Create binary app from python source code using Cython PDF
Sunday, 27 May 2012
If we want to make our Python code runs faster we can compile it to a .so library and still run it from Python (yeah Python knows to do that) but what if we want to make a system executable binary file right from Python source code?

This technique is also usefull to obfuscate/protect/encode Python source code.

We cand o that by using Cython (first we install it):

  cd /usr/ports/lang/cython
  make install clean; rehash


Then we will compile the following Python source code using Cython:

hello.py
#!/usr/bin/env python

print "Content-type: text/html"
print

print """
<html>

<head><title>Our First CGI App</title></head>

<body>

  <h2> Our First CGI App. </h2>
</body>
</html>
"
""


So, we will compile the code using Cython (notice --embed parameter):

  cython --embed hello.py

This will generate a C file called hello.c which we will then compile using GCC:

  gcc -I/usr/local/include/python2.7 -L/usr/local/lib -lpython2.7 hello.c -o hello

And voila, we have our hello binary which we can run:

  ./hello

 
< Prev   Next >

Other BSD Systems

OpenBSD

Misc

Solaris

Polls

Best BSD firewall?