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
2 answers

How does one overwrite the default compile flags for Cython when building with distutils?

I am compiling some cython extensions in linux and noticed that it defaults to using -O2 when building from the distutils mechanism. I was wondering if there was a simple way to change that to a -O3. I have tried using the extra_compile_args on…
Voltaire
  • 1,375
  • 4
  • 17
  • 22
20
votes
2 answers

setup.py for packages that depend on both cython and f2py

I would like to create a setup.py script for a python package with several submodules that depend on both cython and f2py. I have attempted to use setuptools and numpy.distutils, but have so far failed: Using setuptools I am able to compile my…
abergou
  • 441
  • 2
  • 7
20
votes
4 answers

How do you tell pyximport to use the cython --cplus option?

pyximport is super handy but I can't figure out how to get it to engage the C++ language options for Cython. From the command line you'd run cython --cplus foo.pyx. How do you achieve the equivalent with pyximport? Thanks!
BrianTheLion
  • 2,618
  • 2
  • 29
  • 46
20
votes
3 answers

call Cython function from C++

I have a C++ library that has a Python wrapper (written with SWIG). This library allows executing small user-defined code (a callback), such as element-wise operations on a vector. I.e. instead of just a + you can do whatever arbitrary binary…
Adam
  • 16,808
  • 7
  • 52
  • 98
20
votes
3 answers

Cython inline function with numpy array as parameter

Consider code like this: import numpy as np cimport numpy as np cdef inline inc(np.ndarray[np.int32_t] arr, int i): arr[i]+= 1 def test1(np.ndarray[np.int32_t] arr): cdef int i for i in xrange(len(arr)): inc(arr, i) def…
Maxim
  • 463
  • 2
  • 5
  • 12
20
votes
1 answer

How can C++ object lifetimes be correctly managed in Cython?

When writing a Cython wrapper for a C++ library, I've encountered a case where it's not clear how to correctly decide when to delete certain C++ instances. The C++ library looks something like this: #include #include class…
Jean-Paul Calderone
  • 47,755
  • 6
  • 94
  • 122
20
votes
2 answers

Cython Install GCC error

Trying to install Cython on a small VPS running Ubuntu Server. Did sudo apt-get install gcc and then python setup.py install In the Cython directory, but I get this peculiar error. running install running build running build_py running…
DizzyDoo
  • 1,489
  • 6
  • 21
  • 32
20
votes
2 answers

Compiling cython's HelloWorld Example: don't know how to compile C/C++ code on platform

Whenever I try to compile the simpleset cython example helloworld.pyx print "Hello World" setup.py from distutils.core import setup from Cython.Build import cythonize setup( ext_modules = cythonize("helloworld.pyx") ) I am receiving c:\>…
Shaki
  • 349
  • 3
  • 8
20
votes
1 answer

Exposing a file-like object from Cython

I need to expose a file-like object from a C library that i'm wrapping with a Cython module. I want to reuse python's generic io code for stuff like buffering, readline(), etc. The new IO module seems to be just what i need, but actually using it…
bdew
  • 1,330
  • 9
  • 22
20
votes
2 answers

How to test functions cdef'd in Cython?

I have a .pyx file in which I define some functions, e.g. cdef double foo(double a) nogil: return 3. * a How could I unit test the behavior of such functions outside the pyx file? Since they are cdef'd, I am not able to simply import them...
P. Camilleri
  • 12,664
  • 7
  • 41
  • 76
20
votes
2 answers

Cython Fatal Error: Python.h No such file or directory

I have been using Cython to compile my Python files into C files and then use MinGW to create an executable from the C file. Cython works fine, I can type cython test.pyx into the command line and get a C file. The problem is when I attempt to…
Mark Skelton
  • 3,663
  • 4
  • 27
  • 47
20
votes
1 answer

Using a dictionary in Cython , especially inside nogil

I am having a dictionary, my_dict = {'a':[1,2,3], 'b':[4,5] , 'c':[7,1,2]) I want to use this dictionary inside a Cython nogil function . So , I tried to declare it as cdef dict cy_dict = my_dict Up to this stage is fine. Now I need to iterate…
Seja Nair
  • 787
  • 2
  • 9
  • 23
20
votes
3 answers

Cython: Buffer type mismatch, expected 'int' but got 'long'

I'm having trouble passing in this memoryview of integers into this (rather trivial) function. Python is giving me this error: ValueError: Buffer dtype mismatch, expected 'int' but got 'long' Can someone help me understand what's going on?…
hlin117
  • 20,764
  • 31
  • 72
  • 93
20
votes
3 answers

Fastest way to compare row and previous row in pandas dataframe with millions of rows

I'm looking for solutions to speed up a function I have written to loop through a pandas dataframe and compare column values between the current row and the previous row. As an example, this is a simplified version of my problem: User Time …
AdO
  • 445
  • 1
  • 6
  • 17
20
votes
7 answers

Noob-Ready Cython Tutorials

I know a bunch of scripting languages, (python, ruby, lua, php) but I don't know any compiled languages like C/C++ , I wanted to try and speed up some python code using cython, which is essentially a python -> C compiler, aimed at creating C…
spearfire
  • 667
  • 1
  • 7
  • 15