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

Passing memoryview to C function

I have a C function declared as follows: void getIndexOfState(long *p, long C, long G, long B, long *state); Nowadays my cython wrapper code uses the buffer syntax from numpy array: cpdef int getIndexOfState(self, np.ndarray[np.int_t, ndim=1,…
flaviotruzzi
  • 547
  • 4
  • 13
20
votes
2 answers

How to pass a numpy array of string types to a function in Cython

Passing a numpy array of dtype np.float64_t works fine ( below), but I can't pass string arrays. This is what works : # cython_testing.pyx import numpy as np cimport numpy as np ctypedef np.float64_t dtype_t cdef func1 (np.ndarray[dtype_t,…
HeyWatchThis
  • 21,241
  • 6
  • 33
  • 41
19
votes
1 answer

Why is Cython so much slower than Numba when iterating over NumPy arrays?

When iterating over NumPy arrays, Numba seems dramatically faster than Cython. What Cython optimizations am I possibly missing? Here is a simple example: Pure Python code: import numpy as np def f(arr): res=np.zeros(len(arr)) for i in…
Greg A
  • 201
  • 1
  • 2
  • 6
19
votes
1 answer

Coverage of Cython module using py.test and coverage.py

I want to get coverage information of a Cython module using some (unit) tests written in Python. What I have right now is coverage of the tests themselves, i.e. which lines of the tests are executed by running py.test. While nice to look at, I would…
mattmilten
  • 6,242
  • 3
  • 35
  • 65
19
votes
2 answers

Passing Numpy arrays to C code wrapped with Cython

I have a small bit of existing C code that I want to wrap using Cython. I want to be able to set up a number of numpy arrays, and then pass those arrays as arguments to the C code whose functions take standard c arrays (1d and 2d). I'm a little…
JoshAdel
  • 66,734
  • 27
  • 141
  • 140
19
votes
6 answers

Using Cython for game development?

How practical would it be to use Cython as the primary programming language for a game? I am a experienced Python programmer and I absolutely love it, but I'm admittedly a novice when it comes to game programming specifically. I know that typically…
Liam
  • 2,017
  • 3
  • 22
  • 19
19
votes
9 answers

Eclipse pydev warning - "Debugger speedups using cython not found."

I get this warning while running a python program (some basic web automation using selenium): warning: Debugger speedups using cython not found. Run '"/usr/bin/python3.5" …
Clone
  • 3,378
  • 11
  • 25
  • 41
19
votes
1 answer

Customize location of .so file generated by Cython

I have a Cython package with wrappers of a C library. This is the tree structure of the package package/ _api.pxd _wrap.pyx setup.py wrapper/ __init__.py wrap.py Doing python setup.py…
Himanshu Mishra
  • 8,510
  • 12
  • 37
  • 74
19
votes
4 answers

Options for linting Cython code

I have a Cython module that I would like to lint PEP8 style, however pylint syntax errors on Cython cdef syntax. Does anyone have a suggestion about how to maintain Python coding standards for Cython code?
Rich
  • 12,068
  • 9
  • 62
  • 94
19
votes
1 answer

Cython sum v/s mean memory jump

I have been trying to work with Cython and I encountered the following peculiar scenario where a sum function over an array takes 3 times the amount of time that the average of an array takes. Here are my three functions cpdef FLOAT_t…
Richie Abraham
  • 191
  • 1
  • 3
19
votes
2 answers

Numba code slower than pure python

I've been working on speeding up a resampling calculation for a particle filter. As python has many ways to speed it up, I though I'd try them all. Unfortunately, the numba version is incredibly slow. As Numba should result in a speed up, I assume…
jiminy_crist
  • 2,395
  • 2
  • 17
  • 23
19
votes
1 answer

Using distutils and build_clib to build C library

Does anyone have a good example of using the build_clib command in distutils to build an external (non-python) C library from setup.py? The documentation on the subject seems to be sparse or non-existent. My aim is to build a very simple external…
Snorfalorpagus
  • 3,348
  • 2
  • 29
  • 51
19
votes
2 answers

Can't install Kivy: Cython/GCC error

so I tried to install Kivy following the instructions from the official site: $ sudo apt-get install python-setuptools python-pygame python-opengl \ python-gst0.10 python-enchant gstreamer0.10-plugins-good python-dev \ build-essential…
orangesky
  • 305
  • 3
  • 8
19
votes
2 answers

How to keep a C++ class name unmodified with Cython?

I have a C++ class called Foo. If I follow the Cython C++ tutorial I will need to call the Python class differently, PyFoo for example. However I really need to call the Python class Foo as well. How to do that efficiently? Edit: I'm trying to…
ascobol
  • 7,554
  • 7
  • 49
  • 70
18
votes
3 answers

"error: Unable to find vcvarsall.bat" when compiling Cython code

As suggested here, I have succesfully installed Microsoft Visual C++ Compiler for Python 2.7 to compile some Cython code, but: from distutils.core import setup from Cython.Build import cythonize setup(ext_modules = cythonize("module1.pyx")) still…
Basj
  • 41,386
  • 99
  • 383
  • 673