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
11
votes
1 answer

Python 3.4: compile cython module for 64-bit windows

I have a .pyx module that I've been trying to compile for use with 64-bit python 3.4 on Windows through various means but with no success. After a lot of trial and error, it does compile with python setup.py build_ext --inplace…
sjp
  • 382
  • 4
  • 15
11
votes
1 answer

Cython not recognizing c++11 commands

I'm wrapping a C++ class with Python, and I cannot compile any C++11 features with the Cython module. Everything compiles okay when compiling the C++ alone. But when I run this setup.py below: setup( ext_modules = cythonize( …
The Dude
  • 330
  • 6
  • 16
11
votes
1 answer

How can I set Cython compiler flags when using pyximport?

This question (How does one overwrite the default compile flags for Cython when building with distutils?) describes how to set default Cython flags when using distutils. But how do I set default compile flags if I'm just using pyximport? import…
Thomas Johnson
  • 10,776
  • 18
  • 60
  • 98
11
votes
1 answer

cdef statement not allowed here for structure

I have a simple Astruct.pyx with a struct definition (Astruct.pxd): cdef struct A: int x int y int z And a Cython function, that uses it (struct_test.pyx): from random import randint from Astruct cimport A def do(): N = 1000000 …
Student4K
  • 916
  • 2
  • 9
  • 20
11
votes
2 answers

Allocate intermediate multidimensional arrays in Cython without acquiring the GIL

I'm trying to use Cython to parallelize an expensive operation which involves generating intermediate multidimensional arrays. The following very simplified code illustrates the sort of thing I'm trying to do: import numpy as np cimport…
ali_m
  • 71,714
  • 23
  • 223
  • 298
11
votes
2 answers

Using cython to early type class attributes

I'm writing a python class and I would like to accelerate the execution using cython early typing. I get the error "Syntax error in C variable declaration" when I try to cython compile the following: import numpy as np cimport numpy as np class…
user1969231
11
votes
2 answers

Iterating over a list in parallel with Cython

How does one iterate in parallel over a (Python) list in Cython? Consider the following simple function: def sumList(): cdef int n = 1000 cdef int sum = 0 ls = [i for i in range(n)] cdef Py_ssize_t i for i in prange(n,…
clstaudt
  • 21,436
  • 45
  • 156
  • 239
11
votes
1 answer

Cython: Compile Option -O3

How does one overwrite the default compile flags for Cython when building with distutils? My question is similar to this , but the response involved manually running the cython steps - given the progress from 0.12 to 01.9 - is it possible for me to…
pythOnometrist
  • 6,531
  • 6
  • 30
  • 50
11
votes
1 answer

wrapping struct with nested enum - reference in vector template

This is a cross-post of a question I asked in the cython-user group a day and a half ago, but have not yet gotten any replies, so I am trying my luck in a more general forum I have been trying every which way to wrap this following code, with…
jdi
  • 90,542
  • 19
  • 167
  • 203
11
votes
3 answers

Cython and fortran - how to compile together without f2py

FINAL UPDATE This question is about how to write a setup.py that will compile a cython module which accesses FORTRAN code directly, like C would. It was a rather long and arduous journey to the solution, but the full mess is included below for…
tehwalrus
  • 2,589
  • 5
  • 26
  • 33
11
votes
1 answer

Fast(er) numpy fancy indexing and reduction?

I'm trying to use and accelerate fancy indexing to "join" two arrays and sum over one of results' axis. Something like this: $ ipython In [1]: import numpy as np In [2]: ne, ds = 12, 6 In [3]: i = np.random.randn(ne, ds).astype('float32') In [4]: t…
npinto
  • 393
  • 3
  • 7
10
votes
1 answer

What parts of a Numpy-heavy function can I accelerate with Cython

Introductory notes: trying to accelerate Python+Numpy code with Cython is a common problem and this question is an attempt to create a canonical question about what types of operation you can accelerate effectively. Although I try to illustrate with…
DavidW
  • 29,336
  • 6
  • 55
  • 86
10
votes
0 answers

how to debug cython using cygdb?

It is supposedly possible to debug a Python3/Cython project using gdb, after building gdb from source if you configure it with python2.7 as specified in the Cython debugging documentation. However, the example in the documentation: is sometimes…
PDiracDelta
  • 2,348
  • 5
  • 21
  • 43
10
votes
1 answer

Create .pyi file automatically from cython source?

How can I create .pyi files from cython source code (.pyx)?
tamuhey
  • 2,904
  • 3
  • 21
  • 50
10
votes
2 answers

What are differences between Cython's language_level 3 and 3str?

In the upcoming Cython 3.0 version, 3str language_level (which was introduced with Cython 0.29) becomes the new default instead of the current default 2, i.e. if language_level is not set (how to set), we get the following warning: FutureWarning:…
ead
  • 32,758
  • 6
  • 90
  • 153