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

Cython C++ and std::string

What is the best way of using C++ standard std::string from cython? The last cython distribution should make it easy anyway, but I wonder why there are wrappers for std::vector and not for std::string...
dsign
  • 12,340
  • 6
  • 59
  • 82
18
votes
3 answers

Change Cython's naming rules for .so files

I'm using Cython to generate a shared object out of Python module. The compilation output is written to build/lib.linux-x86_64-3.5//.cpython-35m-x86_64-linux-gnu.so. Is there any option to change the naming rule? I want the file to…
hoefling
  • 59,418
  • 12
  • 147
  • 194
18
votes
3 answers

What is the fastest way to compare patches of an array?

I want to compare different areas of a 2 dimensional array $A$ with a given array $b$ of a smaller size. Since I have to do it a lot of times, it is crucial that this is performed very fast. I have a solution that works fine, but I hoped it could be…
mijc
  • 1,189
  • 3
  • 9
  • 16
18
votes
2 answers

OverflowError occurs when using cython with a large int

python 3.4, windows 10, cython 0.21.1 I'm compiling this function to c with cython def weakchecksum(data): """ Generates a weak checksum from an iterable set of bytes. """ cdef long a, b, l a = b = 0 l = len(data) for i in…
user2682863
  • 3,097
  • 1
  • 24
  • 38
18
votes
1 answer

Python crash with minimal Cython module (Python3, Anaconda3, Windows7)

I try to use Cython with Python3 (Anaconda3) under Windows 7. After having solved a bug in distutils by editing the file cygwinccompiler.py (cf. Building minimal cython file with python 3.3 (Anaconda) under windows 7), modules can be built without…
paugier
  • 1,838
  • 2
  • 20
  • 39
18
votes
1 answer

How to use const in Cython

I have read in this link: https://github.com/cython/cython/wiki/FAQ#id35 that my Cython 0.20.1 should be able to support const. However my following code does not compile: cdef const double a = 2.5 print(a) During compilation it says test.pyx:5:5:…
Yuxiang Wang
  • 8,095
  • 13
  • 64
  • 95
18
votes
2 answers

C array vs NumPy array

In terms of performance (algebraic operations, lookup, caching, etc.), is there a difference between C arrays (which can be exposed as a C array, or a cython.view.array [Cython array], or a memoryview of the aforementioned two) and a NumPy arrays…
richizy
  • 2,002
  • 3
  • 21
  • 26
18
votes
1 answer

Python list to Cython

I want to know how to convert normal python list to C list with Cython , process it and return a python list. Like: Python script: import mymodule a = [1,2,3,4,5,6] len = len(a) print(mymodule.process(a,len)) Cython script (mymodule.pyd): cpdef…
Jean-Francois Gallant
  • 13,583
  • 6
  • 20
  • 24
18
votes
2 answers

Convert builtin function type to method type (in Python 3)

Consider a simple function like def increment(self): self.count += 1 which is run through Cython and compiled into an extension module. Suppose now I'd like to make this function a method on a class. For example: class Counter: def…
bfroehle
  • 1,116
  • 10
  • 12
17
votes
1 answer

ModuleNotFoundError: No module named 'Cython'

Why does Cython not want to work ? I've pip installed it and when I try to import it, it just won't work. I'm using MacOS Version 10.12.6 (16G29). ➡️ Desktop pip3 install Cython Collecting Cython Using cached…
AymenTM
  • 529
  • 2
  • 5
  • 17
17
votes
3 answers

Add numpy.get_include() argument to setuptools without preinstalled numpy

I am currently developing a python package that uses cython and numpy and I want the package to be installable using the pip install command from a clean python installation. All dependencies should be installed automatically. I am using setuptools…
pschill
  • 5,055
  • 1
  • 21
  • 42
17
votes
4 answers

Reducing the size of scipy and numpy for aws lambda deployment

I am trying to deploy a python application on aws lambda. It has several large python dependencies, the largest being scipy and numpy. The result is that my application is significantly larger than the allowed 250MB. While trying to find a way to…
joek575
  • 561
  • 5
  • 9
17
votes
1 answer

Fastest way to create a pandas column conditionally

In a Pandas DataFrame, I want to create a new column conditionally based on the value of another column. In my application, the DataFrame typically has a few million lines, and the number of unique conditional values is small, on the order of unity.…
erwanp
  • 1,262
  • 10
  • 19
17
votes
1 answer

Cython --embed flag in setup.py

I am starting to compile my Python 3 project with Cython, and I would like to know if it's possible to reduce my current compile time workflow to a single instruction. This is my setup.py as of now: from distutils.core import setup from…
Guerriky
  • 529
  • 1
  • 5
  • 15
17
votes
2 answers

Magical libc.math.abs in Cython

How does cython manage to cimport abs from libc.math from libc.math cimport abs if it is not there? > grep abs Cython/Includes/libc/math.pxd I even tried deleting everything from that file (I got a 0 length math.pxd) yet it somehow…
Antony Hatchkins
  • 31,947
  • 10
  • 111
  • 111