pip install cython
- setuptools
setup.py
from setuptools import setup from Cython.Build import cythonize setup( name='Hello world app', ext_modules=cythonize( ['hello.pyx'], annotate=True, # enable generation of the HTML annotation files compiler_directives={ 'language_level': 3, # Python3 } ), zip_safe=False, )
- build:
python setup.py build_ext --inplace
- build files(with analysis will generate HTML files):
cython -a hello.pyx
- Jupyter Notebook
- load:
%load_ext Cython
- compile:
%%cython
(with analysis%%cython --annotate
)
- load:
cdef double func(double x) except? -2:
return x ** 2 - x
except -1
: whenever returns-1
, an exception will be assumed to have occurredexcept? -1
: whenever returns-1
, it will check withPyErr_Occurred()
except *
: always check withPyErr_Occurred()
(external C++except +
)