12

I'm new to Cython, but got it working by following this basic guide from the official docs:

All it says is: "Cython has a way to visualise where interaction with Python objects and Python’s C-API is taking place. For this, pass the annotate=True parameter to cythonize(). It produces a HTML file."

I'm very surprised that I couldn't just Google this one or that no one on stackoverflow has asked this. But I can't figure out how to get it to work. It doesn't show specifically what it wants. So I tried the most obvious syntax (in Setup.py):

from distutils.core import setup
from Cython.Build import cythonize

setup(
    ext_modules = cythonize("gpcython.pyx", annotate=True)
)

While this does not throw an error, I do not see any HTML being generated either.

I am on windows using the latest version of Python 3.7 with Cython 0.29.12.

https://cython.readthedocs.io/en/latest/src/tutorial/cython_tutorial.html

Community
  • 1
  • 1
Bruce Nielson
  • 753
  • 8
  • 23
  • 3
    This probably because nothing is built: sadly changing setup.py doesn't lead to a complete rebuild. You need to add `--force`, i.e. `python setup.py build_ext --inplace --force`, then html is next to pyx-file. – ead Jul 08 '19 at 05:06
  • Pretty sure that isn't the problem. I did notice that (thanks for the --force switch!) but I just deleted the build and it started fresh. Same result. No HTML. – Bruce Nielson Jul 08 '19 at 06:16
  • I tried adding: import Cython.Compiler.Options Cython.Compiler.Options.annotate = True No effect – Bruce Nielson Jul 08 '19 at 06:35
  • It works for me with cython 0.28.4. If it doesn't for you and you have a current version you should file the bug: https://github.com/cython/cython/issues – ead Jul 08 '19 at 06:40
  • 1
    https://github.com/cython/cython/issues/3036 – Bruce Nielson Jul 08 '19 at 06:51
  • Not sure what happened, but it's working now. I tried it with a new file today and the HTML got created. So I went back to the old file (which did NOT have an HTML despite using identical commands) and forced a rebuild and the HTML now shows up. – Bruce Nielson Jul 13 '19 at 17:26
  • 2
    setup.py --force did not work for me. However, removing the generated out.c file made the annotate=True work which created the .html file. – mateuszb Aug 12 '19 at 20:11

3 Answers3

15

Here is what I finally used that now seems to work:

from distutils.core import setup
from Cython.Build import cythonize

import Cython.Compiler.Options
Cython.Compiler.Options.annotate = True

setup(
    ext_modules = cythonize("gpcython.pyx", annotate=True)
)
Bruce Nielson
  • 753
  • 8
  • 23
7

Maybe late but I solved this problem like below:

Change .pyx source file or remove .c file and run setup.py again to force cython to rebuild it again.

--force argument may not work.

Seyfi
  • 1,832
  • 1
  • 20
  • 35
6

You can try to remove the generated c or cpp file. If there's no change in pyx, cython won't try to repeat the build. I don't know how cython tracks the build dependencies. I guess it's similar to how make works.

Izana
  • 2,537
  • 27
  • 33