1. Environmental preparation
The idea is to first convert py to c code, and then compile c to so file, so you need to install the following:
pip install cython
sudo apt-get install python-devel
sudo apt-get install gcc
2. Write the test program as follows:
Create a new Test.py file with the following content:
class test:
def __init__(self):
print('init')
def say(self):
print ('hello')
Create a new setup.py file with the following content:
from distutils.core import setup
from Cython.Build import cythonize
setup(ext_modules = cythonize(["Test.py"]))
3. Compile into a .so file
python setup.py build_ext
Build/, callable so files will be generated in the current folder.
4. Call example
Reference: Reference blog