Cython is a software that generate a C build program for Python. What exactly is intended for ? That is most likely the question came out from everyone. Well, one good reason lots of people consider is to accelerate the speed of code process. You may also able to protect Python file by convert to .so file. However, The main objective of the Cython software is to optimize Python code, rebuild it making it operate at C speed.
As usual you need to have root privilege for installation. Access your raspberry pi device, login as pi and password.
Install Cython as follow :
- pip install Cython
wait until process completed.
Convert Python File
Let say you have python file call example.py. Run following command to convert to C extension using Cython.
- Cython exampe.py -o example.c
Unless errors occur, you will notice one new file example.c. if you open the c file, you will see /* Generated by Cython 0.25.2 */ on top (I use Cython 0.25.2). Following Step generate so file.
- gcc -shared -pthread -fPIC -fwrapv -O2 -Wall -fno-strict-aliasing -I/usr/include/python2.7 -o example.so example.c
Execute process on a file
after so file generated, you can access class on the file by using a syntax below from main Python application :
from example import subprocess1, subprocess2
This post is cover the way to use Cython to generate C extension for Python file to gain faster processing time. It is possible to use above process to protecting Python code. Generated (.so) file can be run by main python application. At the end many smart engineer have ability to reverse engineer binary code to uncover source code.