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
314
votes
12 answers

Wrapping a C library in Python: C, Cython or ctypes?

I want to call a C library from a Python application. I don't want to wrap the whole API, only the functions and datatypes that are relevant to my case. As I see it, I have three choices: Create an actual extension module in C. Probably overkill,…
balpha
  • 50,022
  • 18
  • 110
  • 131
183
votes
8 answers

Cython: "fatal error: numpy/arrayobject.h: No such file or directory"

I'm trying to speed up the answer here using Cython. I try to compile the code (after doing the cygwinccompiler.py hack explained here), but get a fatal error: numpy/arrayobject.h: No such file or directory...compilation terminated error. Can anyone…
Noob Saibot
  • 4,573
  • 10
  • 36
  • 60
131
votes
10 answers

How should I structure a Python package that contains Cython code

I'd like to make a Python package containing some Cython code. I've got the the Cython code working nicely. However, now I want to know how best to package it. For most people who just want to install the package, I'd like to include the .c file…
Craig McQueen
  • 41,871
  • 30
  • 130
  • 181
115
votes
4 answers

Compiling with cython and mingw produces gcc: error: unrecognized command line option '-mno-cygwin'

I'm trying to compile a python extension with cython in win 7 64-bit using mingw (64-bit). I'm working with Python 2.6 (Active Python 2.6.6) and with the adequate distutils.cfg file (setting mingw as the compiler) When executing >…
joaquin
  • 82,968
  • 29
  • 138
  • 152
103
votes
3 answers

How to specify Python 3 source in Cython's setup.py?

I am trying to do a "Hello World" program in Cython, following this tutorial http://docs.cython.org/src/tutorial/cython_tutorial.html#cython-hello-world I created helloworld.pyx print("Hello World") and setup.py: from distutils.core import…
rsteward
  • 1,146
  • 2
  • 8
  • 9
101
votes
18 answers

Cannot open include file: 'io.h': No such file or directory

I was trying to compile a simple .pyx file using Cython. print("hello") Here's my setup.py: from distutils.core import setup from Cython.Build import cythonize setup( ext_modules = cythonize("hello.pyx") ) Then I run the command. python…
user2869934
  • 1,419
  • 4
  • 13
  • 18
89
votes
5 answers

Compile main Python program using Cython

I have a Python2.6 program that can load Python modules compiled to .so files using Cython. I used Cython to compile the .py modules to .so files and everything works fine. This is the setup.py file I use with Cython: from distutils.core import…
mozza
  • 965
  • 1
  • 9
  • 9
89
votes
2 answers

What is the recommended way of allocating memory for a typed memory view?

The Cython documentation on typed memory views list three ways of assigning to a typed memory view: from a raw C pointer, from a np.ndarray and from a cython.view.array. Assume that I don't have data passed in to my cython function from outside…
kynan
  • 13,235
  • 6
  • 79
  • 81
72
votes
7 answers

ImportError: No module named 'Cython'

I'm trying do from Cython.Build import cythonize and I get the message ImportError: No module named 'Cython', but I installed the Cython with the comand pip install Cython. What's wrong? Python 3.5 Cython 0.25.2 Windows 8
Tais
  • 959
  • 1
  • 6
  • 12
70
votes
10 answers

Extending python - to swig, not to swig or Cython

I found the bottleneck in my python code, played around with psycho etc. Then decided to write a c/c++ extension for performance. With the help of swig you almost don't need to care about arguments etc. Everything works fine. Now my question: swig…
RSabet
  • 6,130
  • 3
  • 27
  • 26
68
votes
1 answer

Distributing Cython based extensions using LAPACK

I am writing a Python module that includes Cython extensions and uses LAPACK (and BLAS). I am open to using either clapack or lapacke, or some kind of f2c or f2py solution if necessary. What is important is that I am able to call lapack and blas…
jcrudy
  • 3,921
  • 1
  • 24
  • 31
63
votes
3 answers

cython issue: 'bool' is not a type identifier

I'm desperately trying to expose a std::vector class member to a Python class. Here is my C++ class: class Test { public: std::vector test_fail; std::vector test_ok; }; While the access and conversion of test_ok of…
Carmellose
  • 4,815
  • 10
  • 38
  • 56
63
votes
6 answers

How to tell distutils to use gcc?

I want to wrap a test project containing C++ and OpenMP code with Cython, and build it with distutils via a setup.py file. The content of my file looks like this: from distutils.core import setup from distutils.extension import Extension from…
clstaudt
  • 21,436
  • 45
  • 156
  • 239
59
votes
1 answer

Filtering (reducing) a NumPy Array

Suppose I have a NumPy array arr that I want to element-wise filter (reduce) depending on the truth value of a (broadcastable) function, e.g. I want to get only values below a certain threshold value k: def cond(x): return x < k There are a…
norok2
  • 25,683
  • 4
  • 73
  • 99
58
votes
3 answers

Difference between np.int, np.int_, int, and np.int_t in cython?

I am a bit struggled with so many int data types in cython. np.int, np.int_, np.int_t, int I guess int in pure python is equivalent to np.int_, then where does np.int come from? I cannot find the document from numpy? Also, why does np.int_ exist…
colinfang
  • 20,909
  • 19
  • 90
  • 173
1
2 3
99 100