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

Cython, Python and KeyboardInterrupt ignored

Is there a way to interrupt (Ctrl+C) a Python script based on a loop that is embedded in a Cython extension? I have the following python script: def main(): # Intantiate simulator sim = PySimulator() sim.Run() if __name__ ==…
Gauthier Boaglio
  • 10,054
  • 5
  • 48
  • 85
14
votes
3 answers

Most efficient way to build a 1-d array/list/vector of unknown length using Cython? Or should this never be done?

I have a time-critical model that I wrote in Cython. The main function of my Cython extension has one loop, and according to the Cython profiler (where it shows the amount of Python calls in shades of yellow) the only 'yellow' part is currently…
mdscruggs
  • 1,182
  • 7
  • 15
14
votes
1 answer

No module named 'Cython' with pip installation of tar.gz

I use Poetry to build tar.gz and whl files for my example package (https://github.com/iamishalkin/cyrtd) and then try to install package inside pipenv environment. tar.gz installation fails and this is a piece of logs: $ poetry build ... $ pip…
Ivan Mishalkin
  • 1,049
  • 9
  • 25
14
votes
2 answers

How to deal with "clang: error: unsupported option '-fopenmp'" on travis?

I am currently trying to deploy a project using openmp. I have the flag '-fopenmp' on Travis. How could I fix that ? In local I just brew install libopenmp which solved the issue. But not on Travis, what are the options ? Using cython I got the…
BlueSheepToken
  • 5,751
  • 3
  • 17
  • 42
14
votes
1 answer

how to define a list in Cython

I want to transform the below python code in Cython: x_array = [] x_array.append(x_new) I tried the following Cython codes but it gives error: cdef np.ndarray[double, dim=1] x_array x_array.append(x_new) The error shows: Cannot coerce list to…
Michael Fan Zhang
  • 251
  • 1
  • 2
  • 5
14
votes
1 answer

Fastest way to find all unique elements in an array with Cython

I am attempting to find the most performant method to find unique values from a NumPy array. NumPy's unique function is very slow and sorts the values first before finding the unique. Pandas hashes the values using the klib C library which is much…
Ted Petrou
  • 59,042
  • 19
  • 131
  • 136
14
votes
2 answers

Comparing Python accelerators (Cython,Numba,f2py) to Numpy einsum

I'm comparing Python accelerators (Numba, Cython, f2py) to simple For loops and Numpy's einsum for a particular problem (see below). So far Numpy is the fastest for this problem (factor 6x faster), but I wanted some feedback if there are additional…
Michael
  • 486
  • 6
  • 19
14
votes
1 answer

Example program of Cython as Python to C Converter

I found here and here that one can use Cython to convert Python to C, but I cannot find any step-by-step example. Let's say I have a simple function: foo.pyx cdef void foo(double* x): x[0] = 0.0 setup.py from distutils.core import setup from…
Bociek
  • 1,195
  • 2
  • 13
  • 28
14
votes
2 answers

How to use Cython typed memoryviews to accept strings from Python?

How can I write a Cython function that takes a byte string object (a normal string, a bytearray, or another object that follows the buffer protocol) as a typed memoryview? According to the Unicode and Passing Strings Cython tutorial page, the…
Richard Hansen
  • 51,690
  • 20
  • 90
  • 97
14
votes
1 answer

Cython says buffer types only allowed as function local variables even for ndarray.copy()

I am new to Cython and encountered this code snippet: import numpy as np cimport numpy as np testarray = np.arange(5) cdef np.ndarray[np.int_t, ndim=1] testarray1 = testarray.copy() cdef np.ndarray[np.float_t, ndim=1] testarray2 =…
Yuxiang Wang
  • 8,095
  • 13
  • 64
  • 95
14
votes
1 answer

how to pass numpy array to Cython function correctly?

This is described in many places but i simply cannot get it to work. I am calling a C++ function from Cython: cimport numpy as np cdef extern from "test.h" namespace "mytest": void test(double *A, int m) cdef int foo(): cdef…
user248237
14
votes
1 answer

Cython VS C++ Performance Comparison?

I am trying to use Cython to code my project. My plan is to write .dll in C++, and call them from Python via Cython. So I can have high computational performance of C++, while keeping the simplicity of development of Python. As I go further, I am a…
ChangeMyName
  • 7,018
  • 14
  • 56
  • 93
14
votes
2 answers

how to initialize fixed-size integer numpy arrays in Cython?

How can one make empty numpy arrays of type int in Cython? The following works for me for double or float arrays: # make array of size N of type float cdef np.ndarray[float, ndim=1] myarr = np.empty(N) # make array of size N of type int cdef…
user248237
14
votes
2 answers

Propagating c++ exception to cython - python exception

I have a problem with Cython 0.17.1 My function throws a std::runtime_error if a file doesn't exist, I'd like to propagate this exception in some manner to my Cython code. void loadFile(const string &filename) { // some code, if filename doesn't…
linello
  • 8,451
  • 18
  • 63
  • 109
14
votes
3 answers

pickle cython class

I have to save and load a cython class instance. My cython class is this plus several methods: import numpy as np cimport numpy as np cimport cython cdef class Perceptron_avg_my: cdef int wlen,freePos cdef np.ndarray w,wtot,wac,wtotc…
Francesco
  • 323
  • 1
  • 2
  • 10