I have a script_function_cython.pyx file containing a single function:
import numpy as np
import scipy.misc as misc
import matplotlib.pyplot as plt
def my_function(img, kernel):
assert kernel.shape==(3, 3)
res = np.zeros(img.shape)
for i in range(1, img.shape[0]-1):
for j in range(1, img.shape[1]-1):
res[i, j] = np.sum(np.array([[img[i-1, j-1], img[i-1, j], img[i-1, j+1]],
[img[i, j-1], img[i, j], img[i, j+1]],
[img[i+1, j], img[i+1, j], img[i+1, j+1]]])*kernel)
return res
if __name__ == '__main__':
kernel = np.array([[-1, -1, -1], [-1, 3, -1], [-1, -1, -1]])
img = misc.face()[:256, :256, 0]
res = my_function(img, kernel)
plt.figure()
plt.imshow(res, cmap=plt.cm.gray)
I have thus created a setup.py file:
from distutils.core import setup
from Cython.Build import cythonize
setup(
ext_modules = cythonize('script_function_cython.pyx'),
)
Then, I compile it:
python setup.py build_ext --inplace
And install it:
python setup.py install
However, when I try to further import it,
import script_function_cython
I get:
ImportError: No module named script_function_cython
with the --inplace
build there is no need for install. You'll need to import from project directory though.
python setup.py build --inplace
python -c 'import script_function_cython'
should raise no error.