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
44
votes
4 answers

build scipy error cythonize failed

I'm trying to build scipy and I get a RuntimeError: $sudo python setup.py build Processing scipy/cluster/_vq_rewrite.pyx Traceback (most recent call last): File "tools/cythonize.py", line 172, in main() File "tools/cythonize.py",…
Liatz
  • 4,997
  • 7
  • 28
  • 33
43
votes
1 answer

Complex numbers in Cython

What is the correct way to work with complex numbers in Cython? I would like to write a pure C loop using a numpy.ndarray of dtype np.complex128. In Cython, the associated C type is defined in Cython/Includes/numpy/__init__.pxd as ctypedef double…
paugier
  • 1,838
  • 2
  • 20
  • 39
43
votes
4 answers

Cython Numpy warning about NPY_NO_DEPRECATED_API when using MemoryView

I am converting a Cython memoryview to a numpy array (to be able to use it in pure Python code): from libc.stdlib cimport realloc cimport numpy as np DTYPE = np.float64 ctypedef np.float64_t DTYPE_t cpdef np.ndarray[DTYPE_t] compute(DTYPE_t[:,::1]…
Amenhotep
  • 920
  • 1
  • 13
  • 18
42
votes
1 answer

Using Cython To Link Python To A Shared Library

I am trying to integrate a third party library written in C with my python application using Cython. I have all of the python code written for a test. I am having trouble finding an example for setting this up. I have a pyd/pyx file I created…
josephmisiti
  • 9,862
  • 11
  • 56
  • 72
41
votes
4 answers

Compile Cython Extensions Error - Pycharm IDE

Non-zero exit code (1): _pydevd_bundle/pydevd_cython.c:13:20: fatal error: Python.h: No such file or directory compilation terminated. error: command 'x86_64-linux-gnu-gcc' failed with exit status 1 Please help me resolve this error of…
REDDY SARAN LAL
  • 527
  • 1
  • 4
  • 8
41
votes
2 answers

Definition of def, cdef and cpdef in Cython

What is the difference between def, cdef and cpdef when I declare a function? The difference between def and the others is more or less clear. And I've also seen that sometimes it's added the return type in the declaration (cdef void/double/int...…
Pablo
  • 463
  • 1
  • 5
  • 12
40
votes
6 answers

Idiomatic way to do list/dict in Cython?

My problem: I've found that processing large data sets with raw C++ using the STL map and vector can often be considerably faster (and with lower memory footprint) than using Cython. I figure that part of this speed penalty is due to using Python…
ramanujan
  • 5,581
  • 5
  • 30
  • 31
38
votes
1 answer

How do pandas Rolling objects work?

Edit: I condensed this question given that it was probably too involved to begin with. The meat of the question is in bold below. I'd like to know more about the object that is actually created when using DataFrame.rolling or…
Brad Solomon
  • 38,521
  • 31
  • 149
  • 235
38
votes
6 answers

Using Cython with Django. Does it make sense?

Is it possible to optimize speed of a mission critical application developed in Django with Cython? Recently I have read on the internet, that Cython can turn a Python code to C like speed. Is this possible with Django?
fear_matrix
  • 4,912
  • 10
  • 44
  • 65
38
votes
3 answers

How to profile cython functions line-by-line

I often struggle to find bottlenecks in my cython code. How can I profile cython functions line-by-line?
Till Hoffmann
  • 9,479
  • 6
  • 46
  • 64
36
votes
2 answers

Use Cython as Python to C Converter

I have huge Python modules(+8000 lines) .They basically have tons of functions for interacting with a hardware platform via serial port by reading and writing to hardware registers. They are not numerical algorithms. So application is just…
user845459
36
votes
3 answers

Distributing a shared library and some C code with a Cython extension module

I'm trying to take some functions from a large C++ shared library (libbig.so) and expose them to Python via Cython. To do so, I've got a little C++ file (small.cpp) that provides a thin wrapper around the functionality from the shared library that I…
Jeff Hammerbacher
  • 4,226
  • 2
  • 29
  • 36
35
votes
6 answers

Calling C functions in Python

I have a bunch of functions that I've written in C and I'd like some code I've written in Python to be able to access those functions. I've read several questions on here that deal with a similar problem (here and here for example) but I'm confused…
Kitchi
  • 1,874
  • 4
  • 28
  • 46
34
votes
3 answers

How can a #defined C value be exposed to Python in a Cython module?

I'd like to make the integer constants (ACTIVE_TAG, etc) defined here: //island management, m_activationState1 #define ACTIVE_TAG 1 #define ISLAND_SLEEPING 2 #define WANTS_DEACTIVATION 3 #define DISABLE_DEACTIVATION 4 #define DISABLE_SIMULATION…
Jean-Paul Calderone
  • 47,755
  • 6
  • 94
  • 122
33
votes
3 answers

setup_requires with Cython?

I'm creating a setup.py file for a project with some Cython extension modules. I've already gotten this to work: from setuptools import setup, Extension from Cython.Build import cythonize setup( name=..., ..., ext_modules=cythonize([…
Claudiu
  • 224,032
  • 165
  • 485
  • 680