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

Cython in Ipython: ERROR: Cell magic `%%cython` not found

While using cython in ipython notebook, I see the error below. What's wrong? %load_ext cythonmagic /usr/local/lib/python2.7/dist-packages/IPython/extensions/cythonmagic.py:21: UserWarning: The Cython magic has been moved to the Cython package …
aman
  • 1,875
  • 4
  • 18
  • 27
27
votes
1 answer

ValueError: ndarray is not C-contiguous in cython

I have written the following function in cython to estimate the log-likelihood @cython.boundscheck(False) @cython.wraparound(False) def likelihood(double m, double c, np.ndarray[np.double_t, ndim=1, mode='c'] r_mpc not…
Dalek
  • 4,168
  • 11
  • 48
  • 100
27
votes
1 answer

Passing and returning numpy arrays to C++ methods via Cython

There are lots of questions about using numpy in cython on this site, a particularly useful one being Simple wrapping of C code with cython. However, the cython/numpy interface api seems to have changed a bit, in particular with ensuring the passing…
Michael Schubert
  • 2,726
  • 4
  • 27
  • 49
27
votes
3 answers

calling dot products and linear algebra operations in Cython?

I'm trying to use dot products, matrix inversion and other basic linear algebra operations that are available in numpy from Cython. Functions like numpy.linalg.inv (inversion), numpy.dot (dot product), X.t (transpose of matrix/array). There's a…
user248237
26
votes
1 answer

Cython typed memoryviews: what they really are?

The Cython documentation explains very well what they allow for, how you can declare them, and how to use them. However, it is still not clear to me what they really are. For example, a simple assignment from a numpy array like this: my_arr =…
Gioker
  • 649
  • 5
  • 14
26
votes
3 answers

extra_compile_args in Cython

I want to pass some extra options to the Cython compiler by using extra_compile_args. My setup.py: from distutils.core import setup from Cython.Build import cythonize setup( name = 'Test app', ext_modules = cythonize("test.pyx",…
Roman
  • 2,225
  • 5
  • 26
  • 55
26
votes
2 answers

Building Cython-compiled python code with PyInstaller

I am trying to build a Python multi-file code with PyInstaller. For that I have compiled the code with Cython, and am using .so files generated in place of .py files. Assuming the 1st file is main.py and the imported ones are file_a.py and…
Confused
  • 617
  • 1
  • 9
  • 17
26
votes
2 answers

How to create a custom numpy dtype using cython

There are examples for creating custom numpy dtypes using C here: Additionally, it seems to be possible to create custom ufuncs in cython: It seems like it should also be possible to create a dtype using cython (and then create custom ufuncs for…
jcrudy
  • 3,921
  • 1
  • 24
  • 31
25
votes
1 answer

What is the best replacement for python setup.py install when Cython needs to be compiled?

With the latest version of setuptools, the python setup.py install command is being deprecated (see https://blog.ganssle.io/articles/2021/10/setup-py-deprecated.html for more info). (venv) [jon@dev02 py38]$ python setup.py install running…
jon_two
  • 1,073
  • 1
  • 23
  • 34
25
votes
4 answers

Tutorials on optimizing non-trivial Python applications with C extensions or Cython

The Python community has published helpful reference material showing how to profile Python code, and the technical details of Python extensions in C or in Cython. I am still searching for tutorials which show, however, for non-trivial Python…
gotgenes
  • 38,661
  • 28
  • 100
  • 128
25
votes
2 answers

What's the purpose of dictproxy?

The __dict__ of a type is a dictproxy object that is read only. I want to know what's the purpose of it. Is it only for "don't allow modify builtin types"? I found a method that can walk around this. I know it's not a good idea to modify builtin…
HYRY
  • 94,853
  • 25
  • 187
  • 187
25
votes
1 answer

Cython & C++: passing by reference

I am a noob with Cython and C++, so I have a question on argument passing. I want to avoid passing a copy of an argument in the following scenario: # somefile.pyx #distutils: language = c++ from libcpp.vector cimport vector def add_one(vector[int]…
richizy
  • 2,002
  • 3
  • 21
  • 26
25
votes
2 answers

Check if a value exists in an array in Cython

I want to know how to check if a value or a object exists in an array, like in python: a = [1,2,3,4,5] b = 4 if b in a: print("True!") else: print("False") I want to know if something similar already exists in cython. I have a struct object…
Jean-Francois Gallant
  • 13,583
  • 6
  • 20
  • 24
25
votes
1 answer

Wrapping c++ struct template using cython

I am attempting to access the struct template struct Data { double X[dim]; double Val[dim]; }; in cython. I was guessing the correct syntax should be something like: cdef extern from "Lib.h" namespace "LIB": cdef struct Data[int…
Eldila
  • 15,426
  • 23
  • 58
  • 62
24
votes
3 answers

Cython Compilation Error: dynamic module does not define module export function

I am building a package in Cython. I am using the following as the structure for setup.py: from distutils.core import setup from distutils.extension import Extension from Cython.Build import cythonize import numpy import scipy extensions = [ …
Alger Remirata
  • 529
  • 1
  • 5
  • 17