Questions tagged [cython]

Cython is a superset of the Python language for quickly generating Python C extensions.

Cython is a superset of the Python language for quickly generating Python C/C++ extensions. Cython is a pidgin language of Python and C/C++. Unlike pure Python, Cython code is not directly interpreted by the Python interpreter, but is instead used to generate C/C++ code. The generated C/C++ code can then be compiled into a C/C++-extension, which then can be imported by Python code.

Cython syntax was originally based on Python 2, with added type declarations à la C/C++, however, its syntax now supports both Python 2 and 3 language features. Additionally, Cython is capable of generating C/C++ extension code compatible with either Python 2 or Python 3. Its syntax now allows the use of advanced C++ constructs such as template and stl container. Finally, thanks to static typing, Cython code generally executes much faster than Python code.

Cython is freely available under the open source Apache License.

The latest release of Cython is 3.0 alpha 5 (released 2020-05-19). Cython is available from the PyPI package index repository.

5220 questions
12
votes
1 answer

Cython std::pair of two pointers, expected an identifier or literal

Why in Cython is possible to wrap std::pair myPair; but not std::pair myPair; In particular, wrapping in Cython the std::pair is done as follows: pair[int, Foo*] and works smoothly, but when the first element of is also a…
linello
  • 8,451
  • 18
  • 63
  • 109
12
votes
2 answers

how to document cython function on readthedocs

On ReadTheDocs I am not allowed to compile cython extensions, is it possible to configure sphinx in order to extract the docstrings from the cython files without actually compiling them? thanks!
Andrea Zonca
  • 8,378
  • 9
  • 42
  • 70
12
votes
4 answers

running c++ code from python

I want to execute a code helloword.cpp which takes in some argument from console parses those arguments and then prints "hello world" in the console. Now, I want to parse these arguments from a python scripts parsearguments.py So for example: def…
frazman
  • 32,081
  • 75
  • 184
  • 269
11
votes
2 answers

Numpy->Cython conversion: Compile error:Cannot convert 'npy_intp *' to Python object

I have the following code that is to be propperly converted to cython: from numpy import * ## returns winning players or [] if undecided. def score(board): scores = [] checked = zeros(board.shape) for i in xrange(len(board)): …
Ihmahr
  • 1,110
  • 1
  • 16
  • 25
11
votes
1 answer

'easy_install -U cython' fails complaining about vcvarsall.bat and -mno-cygwin

Under Windows, it seems that easy_install with a C dependency isn't very easy. Attempt 1 - vcvarsall.bat errors I am installing cython under Windows7, with MinGw; I modified Windows7's PATH to include C:\MinGw\bin. At this point, I tried to…
Mike Pennington
  • 41,899
  • 19
  • 136
  • 174
11
votes
0 answers

How to debug Cython code in a modern IDE like VSCode?

I'd like to know how people debug their Cython code, do they just use terminal debugging as suggested in the Doc? This answer suggests it is possible with DDD, not to knock DDD, but I don't want to change my whole workflow for debugging Cython…
Daniel Farrell
  • 9,316
  • 8
  • 39
  • 62
11
votes
1 answer

What is the precedence of python compiled files in imports?

Python files are compiled to bytecode (*.pyc). Using Cython you can compile them to machine code (*.so in Linux). If you use have both files in the same folder, under the same name what is the precedence between them? Is there an automatic way to…
pmav99
  • 1,909
  • 2
  • 20
  • 27
11
votes
2 answers

Compiling cython with gcc: No such file or directory from #include "ios"

Given a file docprep.pyx as simple as from spacy.structs cimport TokenC print("loading") And trying to cythonize it via cythonize -3 -i docprep.pyx I get the following error message docprep.c:613:10: fatal error: ios: No such file or directory …
tsorn
  • 3,365
  • 1
  • 29
  • 48
11
votes
2 answers

Cython - converting pointers to arrays into Python objects

Alright, I am so close to finishing this I can taste it. Over the past few week or so, I've been attempting to create a Python extension to interface with a library written in C++ via Cython. With a little help from the guys here and a couple of…
Josiah
  • 3,266
  • 24
  • 24
11
votes
2 answers

Cython - implementing callbacks

I have been working with Cython in an attempt to interface with a library written in c++. So far things are going pretty good, and I can effectively use MOST functions within the library. My only problem lies within implementing callbacks. The…
Josiah
  • 3,266
  • 24
  • 24
11
votes
1 answer

Compiling pyx files with dependencies in different packages

I am having problems compiling cdef-ed types in different packages and I couldn't find an explanation in cython docs. I have this setup.py in the root of my python src tree: from distutils.core import setup from distutils.extension import…
masterpiga
  • 860
  • 7
  • 13
11
votes
1 answer

How to host cython web app on heroku?

At the current moment I'm playing with Cython and trying to figure out how I can host a Cython Flask app (for example) on heroku. Let's say my project looks like this (after cython compile): _/cythonheroku |-- requirements.txt |-- run.py |--…
Mark Cholak
  • 303
  • 2
  • 11
11
votes
1 answer

Cython attemps to compile twice, and fails

I have a setup.py file that is very similar to the one shown here: https://stackoverflow.com/a/49866324/4080129. It looks like this: from distutils.core import setup, Extension from Cython.Build import cythonize import numpy sources =…
Martino
  • 729
  • 6
  • 18
11
votes
2 answers

What are the differences between a cpdef and a cdef wrapped in a def?

In the Cython docs there is an example where they give two ways of writing a C/Python hybrid method. An explicit one with a cdef for fast C access and a wrapper def for access from Python: cdef class Rectangle: cdef int x0, y0 cdef int x1,…
Paul Panzer
  • 51,835
  • 3
  • 54
  • 99
11
votes
1 answer

Cannot use line_profiler with Cython

Based on the answer to this question I was trying to use the line_profiler with a cythonized function. On the abovementioned question, the accepted answer gives us an example on how to use it with jupyter notebook. However, when I try to build the…
Pedro Braz
  • 2,261
  • 3
  • 25
  • 48