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

In a setup.py involving Cython, if install_requires, then how can from library import something?

This doesn't make sense to me. How can I use the setup.py to install Cython and then also use the setup.py to compile a library proxy? import sys, imp, os, glob from setuptools import setup from Cython.Build import cythonize # this isn't installed…
101010
  • 14,866
  • 30
  • 95
  • 172
11
votes
3 answers

Compiling Python to C using Cython

I'm trying to compile python source code foo.py to C using cython. In foo.py: print "Hello World" The command I'm running is cython foo.py. The problem is that when compiling foo.c using gcc, I get the error: undefined reference to 'main'.
Forge
  • 6,538
  • 6
  • 44
  • 64
11
votes
3 answers

How to account for column-contiguous array when extending numpy with C

I have a C-function to normalize the rows of an array in log-space (this prevents numerical underflow). The prototype of my C-function is as follows: void normalize_logspace_matrix(size_t nrow, size_t ncol, double* mat); You can see that it takes a…
oceanhug
  • 1,352
  • 1
  • 11
  • 24
11
votes
1 answer

cython in jupyter notebook

I am getting errors when loading a Cython file in Jupyter Notebook. Any ideas? %load_ext Cython import numpy as np cimport numpy as np import cython Just a simple error message: File "", line 5 cimport numpy as…
john mangual
  • 7,718
  • 13
  • 56
  • 95
11
votes
1 answer

Cython: Memory view of freed memory

In Cython code, I can allocate some memory and wrap it in a memory view, e.g. like this: cdef double* ptr cdef double[::1] view ptr = PyMem_Malloc(N*sizeof('double')) view = ptr If I now free the memory using PyMem_Free(ptr),…
jmd_dk
  • 12,125
  • 9
  • 63
  • 94
11
votes
2 answers

Running Cython in Jupyter iPython

Running an iterative loop for a geometric progression for a time trial, using the Cython interface. Get an error on compile (shift-enter): CompileError: command 'gcc' failed with exit status 1 %load_ext Cython %%cython def…
rrg
  • 655
  • 2
  • 6
  • 24
11
votes
1 answer

Error compiling Cython file: pxd not found in package

Trying to cimport pxd definitions from other packages. Simple example, a.pxd file: cdef inline void a(): print "a" b.pyx file: cimport a def b(): a.a() Until here, everything is ok, and $ cython b.pyx works. If i move a.pxd to a folder,…
André Panisson
  • 876
  • 9
  • 22
11
votes
1 answer

Cython: using imported class in a type declaration

I'm writing a Cython 0.23 program, and I can't figure out how to use a cdef class that I import from a different module in a type declaration. Here is a snippet that reproduces the problem. test.py: import pyximport pyximport.install() from…
Pastafarianist
  • 833
  • 11
  • 27
11
votes
1 answer

Binding C array to Numpy array without copying

I am writing a Python class that will wrap a C module containing a C struct. I am using the Cython language (a super-set language of Python and C). The C struct is malloc'd in the constructor and contains an array that I want to use in Python. The…
Max Segal
  • 1,955
  • 1
  • 24
  • 53
11
votes
0 answers

Cython & Python Project Test Driven Development and .pyx file structure advice

What is the best way to structure a python/cython project such that I can unit test code that resides in .pyx files? Is it possible to unit test this code in place or will refactoring allow me to achieve this in another way? I am new to cython but…
rnoodle
  • 534
  • 2
  • 5
  • 21
11
votes
2 answers

Storing unsafe C derivative of temporary Python reference error in Cython

I was writing code to store a (potentially) very large integer value into an array of chars referenced by a pointer. My code looks like this: cdef class Variable: cdef unsigned int Length cdef char * Array def __cinit__(self, var,…
Woody1193
  • 7,252
  • 5
  • 40
  • 90
11
votes
2 answers

Compile Cython on pip package build

I'm developing a Python package, EcoPy, that is mostly written in pure Python. The main folder is called ecopy. There's a subfolder called regression that has a Cython file that's already been built. The main setup.py file includes the…
Nate
  • 1,888
  • 3
  • 18
  • 26
11
votes
1 answer

String in Cython functions

I'd like to do this to pass a string to a Cython code: # test.py s = "Bonjour" myfunc(s) # test.pyx def myfunc(char *mystr): cdef int i for i in range(len(mystr)): # error! len(mystr) is not the length of string print…
Basj
  • 41,386
  • 99
  • 383
  • 673
11
votes
2 answers

How to know if my code is running through Cython or standard Python interpreter?

Is there a reliable way to check at runtime if some python code is "cythonized" or if it us running into a standard Python interpreter?
user3274434
  • 151
  • 1
  • 10
11
votes
3 answers

How to use 128 bit integers in Cython

On my 64 bit computer the long long type has 64 bits. print(sizeof(long long)) # prints 8 I need to use 128 bit integers and luckily GCC supports these. How can I use these within Cython? The following doesn't work. Compiling foo.pyx containing…
chtenb
  • 14,924
  • 14
  • 78
  • 116