There are several ways of using Cython with Python.
First version (standard): you write your Cython code
mylib.pyx, then you run thissetup.py:from distutils.core import setup
from Cython.Build import cythonize
setup(ext_modules = cythonize("mylib.pyx"))that compiles it into a
.pydfile, and then, in the mainmyprogram.py, you doimport mylib.That's the standard way of working with Cython.
Second version ("on the fly compiling, with
pyximport") :You write
mylib.pyx, and you don't need any setup.py, you just do this in the main Python code:import pyximport; pyximport.install()
import mylibThat's it! The compilation will be done on the fly on the startup of the Python program!
Question:
Is there a "third version" ("easier on the fly compiling"):
# myprogram.py
import pyximport
pyximport.compile("""
# Here is directly the Cython code inserted in the main Python code
def mycythonfunction(int a, int b):
cdef int i, j,
...
""")
res = mycythonfunction(3, 2)
i.e. with the Cython code directly inserted in the main Python code?