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
3
votes
0 answers

Can I use Python type hints to compile code?

Maybe more of a bit of a random curiosity than a proper technical question, but I was wondering whether there were any tools within the Python ecosystem that both support static typing via type hints (e.g. mypy) and code compilation (like with…
Liam Deacon
  • 904
  • 1
  • 9
  • 25
3
votes
1 answer

Remove Cython assertion option in setup.py

My Cython (.pyx) file contains assert and I would like to remove it when I compile the file. I found this post and edited my setup.py as follows. from distutils.core import setup from distutils.extension import Extension from Cython.Distutils import…
user51966
  • 967
  • 3
  • 9
  • 21
3
votes
1 answer

Calling an MPI dependent Fortran module via Cython

I am trying to call a Fortran library, which uses Open MPI, from Python. I chose to use Cython as the layer between Python and Fortran. I got a minimal working example running from this answer, but I have problems when it comes to using Open…
LSchueler
  • 1,414
  • 12
  • 23
3
votes
1 answer

How do I set all the values of a struct in one go?

If I have the struct cdef struct Interval: unsigned int start unsigned int end unsigned int index I can assign values to it like i.start = 1 but can I set all the values (start, end, index) in one go?
The Unfun Cat
  • 29,987
  • 31
  • 114
  • 156
3
votes
1 answer

Cython: 'PyxImporter' object has no attribute 'find_spec'

I'm trying to integrate a Cython module into my project and I'm having trouble getting it to compile correctly. I have traced my problem to this minimal example: Say I have two files a.py and b.pyx located in the same directory, if I then do the…
Peter
  • 2,919
  • 1
  • 16
  • 35
3
votes
1 answer

How to import cython generated files in golang using cgo

I cythonized my python file to example.c and example.so I can export these files as a python module and access the function in python as testing.py: import example example.test() this way i can easily import my cythonized python files in…
mohd.gadi
  • 495
  • 1
  • 7
  • 15
3
votes
1 answer

How to convert a C++ vector to a numpy vector in Cython whilst minimising Python interpreter interaction?

Specifically: How to provide numpy.ndarray as a function output data type and How to use cimport numpy instead of import numpy to create an array without Python overhead? The code below works if numpy.ndarray is removed from the line cdef…
Greg
  • 8,175
  • 16
  • 72
  • 125
3
votes
1 answer

Translating python class into cython

I have the following codes in Python: class DisjointSet: def __init__(self, n): self.parent = list(range(n)) self.rank = [0 for x in range(n)] def find(self, v): if v != self.parent[v]: self.parent[v] =…
Physicist
  • 2,848
  • 8
  • 33
  • 62
3
votes
2 answers

Dask with cython in Juypter: ModuleNotFoundError: No module named '_cython_magic

I am getting: KilledWorker: ("('from_pandas-1445321946b8a22fc0ada720fb002544', 4)", 'tcp://127.0.0.1:45940') I've read the explanation about the latter error message, but this is all confusing coming together with the error message at the top of…
matanster
  • 15,072
  • 19
  • 88
  • 167
3
votes
0 answers

Cython: avoid compiling all C/C++ files on each extension. Objective: distribute in PyPi

The Cython project I am currently working on includes some 20 C++ files that act as C++ implementation. On top I have built three Extension Modules written in Cython, each of them capturing different but interconnected functionalities of the C++…
ibarrond
  • 6,617
  • 4
  • 26
  • 45
3
votes
3 answers

Python, efficient paralell operation using a dict

First sorry for my not perfect English. My problem is simple to explain, I think. result={} list_tuple=[(float,float,float),(float,float,float),(float,float,float)...]#200k tuples threshold=[float,float,float...] #max 1k values for tuple in…
3
votes
2 answers

Some standard C library math operations don't play nice with noGIL

I have a pyx file containing the following code: cimport cython from libc.math cimport sqrt, abs from libc.stdio cimport printf from cython.parallel import prange cdef double my_abs(double a): return sqrt(a*a) cpdef loop(int a): cdef int…
Takoda
  • 354
  • 3
  • 12
3
votes
1 answer

Singleton is not working in Cython

This is how i define Singleton. class Singleton(type): _instances = {} def __call__(cls, *args, **kwargs): if cls not in cls._instances: cls._instances[cls] = super(Singleton, cls).__call__(*args, **kwargs) return…
mcsy
  • 43
  • 6
3
votes
1 answer

Allocating with malloc in Cython

Is there any difference in Cython between allocating memory like this from libc.stdlib cimport malloc cdef int *ar = malloc(100 * sizeof(int)) and like this from libc.stdlib cimport malloc ar = malloc(100 * sizeof(int))
Konstantin
  • 2,937
  • 10
  • 41
  • 58
3
votes
1 answer

Using C union with SSE intrinsics in Cython results in SIGSEGV

I have following snippet of C code that I want wrap with Cython and use in my Python program: typedef union my_mat4 { float Elements[4][4]; #ifdef MAT4_MATH__USE_SSE __m128 Rows[4]; #endif } my_mat4; static inline my_mat4…
Andrej Kesely
  • 168,389
  • 15
  • 48
  • 91