How to use annotate=True on Cythonize()
Asked Answered
A

3

14

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

Ard answered 8/7, 2019 at 4:37 Comment(7)
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.Canard
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.Ard
I tried adding: import Cython.Compiler.Options Cython.Compiler.Options.annotate = True No effectArd
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: github.com/cython/cython/issuesCanard
github.com/cython/cython/issues/3036Ard
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.Ard
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.Impolite
A
17

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)
)
Ard answered 13/7, 2019 at 17:28 Comment(0)
L
9

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.

Landy answered 11/12, 2020 at 18:46 Comment(0)
S
7

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.

Superhighway answered 25/3, 2020 at 18:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.