Basics

basic usage of `cython`

before read this document you can check code here

Setup

install cython in your local. I used python@3.9.10

# create venv
python3 -m venv venv
source ./venv/bin/activate
pip3 install cython

# create empty files
touch setup.py main.py helloworld.pyx

type like below inside of setup.py. every time we build cython script, we use this.

setup.py
from setuptools import setup
# you might be got error with below line.
# but there is no issue when build `cython` script. so go ahead.
from Cython.Build import cythonize

# define build target
setup(
    ext_modules=cythonize("helloworld.pyx")
)

in helloworld.pyx write down like below. nothing special:

in main.py we call built result:

Build

after finished setup, let's compile cython script. command like below:

after build cython you can see helloworld.c file and /build. and other stuffs. in this time, we do not cover about that.

Run

let's call cython.

References

Last updated