Basic sample with Cython’s glob

Let’s see how to use Cythong for extension module from python

First, Check out if your cython is installed. if not, do as follows:

pip3 install cython

and then make helloworld.pyx to generate *.so file.

1
2
3
# in hellowworld.pyx

print("Hello World")

Second, setup.py which is like a python Makefile.

1
2
3
4
5
6
7
8
9
from distutils.core import setup
from distutils.extension import Extension
from Cython.Build import cythonize

extensions = [Extension("*", ["*.pyx"])]

setup(
 ext_modules = cythonize(extensions)
)

And the on prompt, type in as follows:

python3 setup.py build_ext --inplace

The following is the result of running the command above.

1
2
3
4
5
6
7
$ ./run.sh
running build_ext
building 'helloworld' extension
creating build
creating build/temp.linux-x86_64-3.5
x86_64-linux-gnu-gcc -pthread -DNDEBUG -g -fwrapv -O2 -Wall -Wstrict-prototypes -g -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 -fPIC -I/home/hyunyoung2/Labs/Konltk/Cython/env/include -I/usr/include/python3.5m -c helloworld.c -o build/temp.linux-x86_64-3.5/helloworld.o
x86_64-linux-gnu-gcc -pthread -shared -Wl,-O1 -Wl,-Bsymbolic-functions -Wl,-Bsymbolic-functions -Wl,-z,relro -Wl,-Bsymbolic-functions -Wl,-z,relro -g -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 build/temp.linux-x86_64-3.5/helloworld.o -o /home/hyunyoung2/Labs/Konltk/Cython/Basic_sample_about_Cython_glob/helloworld.cpython-35m-x86_64-linux-gnu.so

which will leave a file in your local directory called helloworld.so in linux or helloworld.pyd in windows, Now to use this fileL: start the python interpreter and simply import it as if it was a regular python module.

Let’s chekck if it works

1
2
3
4
5
6
 python3
Python 3.5.2 (default, Nov 23 2017, 16:37:01) 
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import helloworld
Hellow World

Reference