0

I recently came across sentdex tutorial for cython. While trying out his tutorial codes the thing I noticed is how we will debug our cython code before compiling it.

The original code we can debug by running it example_original.py in our interpreter.

#example_original.py
def test(x):
    y = 0
    for i in range(x):
        y += i
    return y
print test(20)

But the cythonized code dosent work. This is the two ways which I tried

1) py file

#example_cython.py
cpdef int test(int x):
    cdef int y = 0
    cdef int i
    for i in range(x):
        y += i
    return y

print test(5)

Error

  File "example_cython.py", line 3
    cpdef int test(int x):
            ^
  SyntaxError: invalid syntax

2) pyx file

#example_cython.pyx
cpdef int test(int x):
    cdef int y = 0
    cdef int i
    for i in range(x):
        y += i
    return y

print test(5)

Error

./example_cython: not found

What is the right way to debug cython codes before compiling it?

Eka
  • 14,170
  • 38
  • 128
  • 212
  • I don't think this question makes much sense. Cython is a compiled language. To debug it you have to compile it. – DavidW Oct 06 '17 at 18:07

1 Answers1

1

To check that your Cython code is syntactically correct, and that there are no obvious problems detectable by static analysis, you can use the cython or cythonize command line tool.

cython path/to/file.pyx to run the Cython compiler, translating the Cython code into C code saved in a file with the same name with a .c extension instead of .pyx. If problems are detected, they'll be written to the STDOUT/STDERR, though a .c file may still be generated.

You can pass the -a option to this program to have the compiler generate an additional HTML file which will highlight parts of your code which incur additional Python overhead.

This does not actually compile your code into a shared library that you can import with Python. You'll need to invoke a C compiler on the generated C code, usually through Python's setuptools/distutils toolchain.

mobiusklein
  • 1,403
  • 9
  • 12