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

Using setuptools to create a cython package calling an external C library

I am trying to compile, install and run a package that we'll call myPackage. It contains a *.pyx file that calls the function fftw_set_timelimit() from library fftw. Currently, when I run a script clientScript.py that imports the package I obtain…
Gael Lorieul
  • 3,006
  • 4
  • 25
  • 50
24
votes
1 answer

Implementing faster python inner product with BLAS

I found this useful tutorial on using low-level BLAS functions (implemented in Cython) to get big speed improvements over standard numpy linear algebra routines in python. Now, I've successfully gotten the vector product working fine. First I save…
moustachio
  • 2,924
  • 3
  • 36
  • 68
24
votes
1 answer

Python Setup.py Build_Ext --inplace

I am building a Cython program (called for ex. testpackage) using the command: python setup.py build_ext --inplace In a folder like /home/USER/Documents/testpackage/. The build runs successfully but when I cd into another folder, I can no longer…
G01
  • 421
  • 1
  • 4
  • 14
24
votes
4 answers

Call python code from c via cython

So I'd like to call some python code from c via cython. I've managed to call cython code from c. And I can also call python code from cython. But when I add it all together, some things are missing. Here is my python code (quacker.pyx): def…
pius
  • 2,344
  • 1
  • 23
  • 25
24
votes
3 answers

Wrap C++ lib with Cython

I'm new to Cython and I'm trying to use Cython to wrap a C/C++ static library. I made a simple example as follow. Test.h: #ifndef TEST_H #define TEST_H int add(int a, int b); int multipy(int a, int b); #endif Test.cpp #include "test.h" int…
YuQing Zhang
  • 779
  • 1
  • 5
  • 17
24
votes
1 answer

how to reload a cython module interactively using pyximport

When writing python code, my typical workflow is to use the interactive prompt and do something like write function repeat until working: test function edit function Once I'm pretty sure everything's ok, I'll run the code in non-interactive…
kith
  • 5,486
  • 1
  • 21
  • 21
24
votes
3 answers

How do I use Sphinx with Cython?

I have recently Cythonized a project of mine by renaming all of the modules (except for the top-level __init__.py) to *.pyx, and by putting ext_modules = [Extension('foo', ['foo.pyx'])] in setup.py. Building and installing works fine. However,…
Dan Stahlke
  • 1,417
  • 1
  • 15
  • 20
23
votes
2 answers

Cython: ImportError: No module named 'myModule': how to call a cython module containing a cimport to another cython nodule?

I'm trying to import a cython module data.pyx into another cython module user.pyx. Everything compile fine, but when I try to call user.pyx in a python module, I am getting the error 'ImportError: No module named data'. Everything is in the same…
user1030312
23
votes
1 answer

How to speed up pandas with cython (or numpy)

I am trying to use Cython to speed up a Pandas DataFrame computation which is relatively simple: iterating over each row in the DataFrame, add that row to itself and to all remaining rows in the DataFrame, sum these across each row, and yield the…
Alexander
  • 105,104
  • 32
  • 201
  • 196
23
votes
1 answer

Cython: are typed memoryviews the modern way to type numpy arrays?

Let's say I'd like to pass a numpy array to a cdef function: cdef double mysum(double[:] arr): cdef int n = len(arr) cdef double result = 0 for i in range(n): result = result + arr[i] return result Is this the modern way…
bzm3r
  • 3,113
  • 6
  • 34
  • 67
23
votes
1 answer

Fast n-dimensional sparse array in Python / Cython

I have an application that involves large n-dimensional arrays which are very sparse. scipy.sparse has a useful 'vectorized getting and setting' feature, so that Cython can be used to populate a sparse matrix quickly. Of course the scipy package…
Neal Hughes
  • 552
  • 3
  • 13
23
votes
5 answers

Correct way to generate random numbers in Cython?

What is the most efficient and portable way to generate a random random in [0,1] in Cython? One approach is to use INT_MAX and rand() from the C library: from libc.stdlib cimport rand cdef extern from "limits.h": int INT_MAX cdef float randnum =…
user248237
23
votes
4 answers

Passing C++ pointer as argument into Cython function

cdef extern from "Foo.h": cdef cppclass Bar: pass cdef class PyClass: cdef Bar *bar def __cinit__(self, Bar *b) bar = b This will always give me something like: Cannot convert Python object argument to type 'Bar *' Is…
meteoritepanama
  • 6,092
  • 14
  • 42
  • 55
23
votes
1 answer

How to compile and link multiple python modules (or packages) using cython?

I have several python modules (organized into packages), which depend on each other. e.g. Module1 Module2: imports Module1 Module3 Module4: imports Module3, Module 2, Module 1 Let's assume the relevant interface to develop applications is in…
Mannaggia
  • 4,559
  • 12
  • 34
  • 47
22
votes
2 answers

Cython buffer declarations for object members

I want to have a Cython "cdef" object with a NumPy member, and be able to use fast buffer access. Ideally, I would do something like: import numpy as np cimport numpy as np cdef class Model: cdef np.ndarray[np.int_t, ndim=1] A def sum(self): …
Brendan OConnor
  • 9,624
  • 3
  • 27
  • 25