Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use annotate=True on Cythonize()

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

like image 343
Bruce Nielson Avatar asked Jul 08 '19 04:07

Bruce Nielson


People also ask

How do you Cythonize a code in Python?

To make your Python into Cython, first you need to create a file with the . pyx extension rather than the . py extension. Inside this file, you can start by writing regular Python code (note that there are some limitations in the Python code accepted by Cython, as clarified in the Cython docs).


1 Answers

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)
)
like image 150
Bruce Nielson Avatar answered Sep 18 '22 04:09

Bruce Nielson