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
57
votes
4 answers

Making an executable in Cython

Been playing with cython. Normally program in Python, but used C in a previous life. I can't figure out how to make a free-standing executable. I've downloaded cython, and I can make a .pyx file (that's just a normal Python file with a .pyx…
Paul Nelson
  • 1,291
  • 4
  • 13
  • 20
56
votes
4 answers

Can Cython compile to an EXE?

I know what Cythons purpose is. It's to write compilable C extensions in a Python-like language in order to produce speedups in your code. What I would like to know (and can't seem to find using my google-fu) is if Cython can somehow compile into…
ThantiK
  • 1,582
  • 4
  • 17
  • 31
51
votes
4 answers

Writing a Python extension in Go (Golang)

I currently use Cython to link C and Python, and get speedup in slow bits of python code. However, I'd like to use goroutines to implement a really slow (and very parallelizable) bit of code, but it must be callable from python. (I've already seen…
tehwalrus
  • 2,589
  • 5
  • 26
  • 33
50
votes
9 answers

Cython compiled C extension: ImportError: dynamic module does not define init function

I have just compiled part of my C library as an extension using Cython, as a "proof of concept". I managed to hack the code (const correctnes problems etc aside), to finally get an extension built. However, when I attempted to import the newly…
Homunculus Reticulli
  • 65,167
  • 81
  • 216
  • 341
50
votes
3 answers

Improve Pandas Merge performance

I specifically dont have performace issue with Pands Merge, as other posts suggest, but I've a class in which there are lot of methods, which does a lot of merge on datasets. The class has around 10 group by and around 15 merge. While groupby is…
Debasish Kanhar
  • 1,123
  • 2
  • 15
  • 27
50
votes
3 answers

Make distutils look for numpy header files in the correct place

In my installation, numpy's arrayobject.h is located at …/site-packages/numpy/core/include/numpy/arrayobject.h. I wrote a trivial Cython script that uses numpy: cimport numpy as np def say_hello_to(name): print("Hello %s!" % name) I also have…
Vebjorn Ljosa
  • 17,438
  • 13
  • 70
  • 88
49
votes
7 answers

Docker-compose no longer building image (AttributeError: cython_sources)

I am building a django-react site and suddenly my docker-compose no longer builds despite any changes to requirements or image versions. My requirements.txt looks as…
Marco Sousa
  • 659
  • 1
  • 6
  • 12
49
votes
2 answers

Cython: (Why / When) Is it preferable to use Py_ssize_t for indexing?

This is a follow-up to this question. (Why / When) Is it preferable to use Py_ssize_t for indexing? In the docs I just found # Purists could use "Py_ssize_t" which is the proper Python type for # array indices. -> Does that mean always when…
embert
  • 7,336
  • 10
  • 49
  • 78
48
votes
3 answers

Are there advantages to use the Python/C interface instead of Cython?

I want to extend python and numpy by writing some modules in C or C++, using BLAS and LAPACK. I also want to be able to distribute the code as standalone C/C++ libraries. I would like this libraries to use both single and double precision float.…
Edouard
  • 1,463
  • 1
  • 11
  • 13
46
votes
2 answers

Why is np.dot so much faster than np.sum?

Why is np.dot so much faster than np.sum? Following this answer we know that np.sum is slow and has faster alternatives. For example: In [20]: A = np.random.rand(1000) In [21]: B = np.random.rand(1000) In [22]: %timeit np.sum(A) 3.21 µs ± 270 ns…
Simd
  • 19,447
  • 42
  • 136
  • 271
46
votes
4 answers

Simple wrapping of C code with cython

I have a number of C functions, and I would like to call them from python. cython seems to be the way to go, but I can't really find an example of how exactly this is done. My C function looks like this: void calculate_daily ( char *db_name, int…
Jose
  • 2,089
  • 2
  • 23
  • 29
45
votes
6 answers

Running Cython in Windows x64 - fatal error C1083: Cannot open include file: 'basetsd.h': No such file or directory

I have been trying to install Cython for Python 2.7 on my Window 7 system. In particular, I prefer everything in 64 bits. (In case you wonder, I need Cython because Cython is one of the components I need for another package for some specialized…
Argyll
  • 8,591
  • 4
  • 25
  • 46
45
votes
1 answer

Cython: cimport and import numpy as (both) np

In the tutorial of the Cython documentation, there are cimport and import statements of numpy module: import numpy as np cimport numpy as np I found this convention is quite popular among numpy/cython users. This looks strange for me because they…
ywat
  • 2,757
  • 5
  • 24
  • 32
44
votes
5 answers

Numpy vs Cython speed

I have an analysis code that does some heavy numerical operations using numpy. Just for curiosity, tried to compile it with cython with little changes and then I rewrote it using loops for the numpy part. To my surprise, the code based on loops was…
Hernan
  • 5,811
  • 10
  • 51
  • 86
44
votes
7 answers

Cython Speed Boost vs. Usability

I just came across Cython, while I was looking out for ways to optimize Python code. I read various posts on Stack Overflow, the python wiki and read the article "General Rules for Optimization". Cython is something which grasps my interest the…
user277465