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

creating a .pxd file for a python module

One way of using cython, the Python to C compiler, is to instead of rewriting your python code in Cython, just writing a .pxd file with the same name of your module where you declare the type of your variables, as explained in here Does anyone know…
fccoelho
  • 6,012
  • 10
  • 55
  • 67
3
votes
1 answer

Python vs. MATLAB computing an integral to infinity with different results, alternative (i.e. expand Gauss-Legendre quadrature to -x-> Infinity)?

I am getting inconsistent results between MATLAB's quadgk and Python's quad routine for an integral from (-x or 0) -> infinity. I believe the MATLAB version is correct (based on a sense check of switching the flag parameter from 1 to -1) whereas…
Matt
  • 2,602
  • 13
  • 36
3
votes
1 answer

Cython program is slower than plain Python (10M options 3.5s vs 3.25s Black Scholes) - what am I missing?

Okay here's my first Cython program below, the code to price European options on futures (Black Scholes without a dividend). It runs in 3.5s on 10M options, versus the code I posted below with straight numpy Python 3.25s. Can anyone point out why…
Matt
  • 2,602
  • 13
  • 36
3
votes
0 answers

Is the python garbage collector guaranteed to be non-copying?

The reason I ask is because it is possible to operate on memory views in Cython without the GIL, and without somehow marking them to not be moved. For example: def f(double[:] z): cdef int i, n = len(z) with nogil: for i in…
mrip
  • 14,913
  • 4
  • 40
  • 58
3
votes
1 answer

Import Cython header from another package

I have a package called A: A/ __init__.py _test.pyx I have the source for another package called B: B/ __init__.py _wrapper.pxd _other.pxd _other.pyx _wrapper.pxd wraps a C library. I'd like to cimport _wrapper.pxd in A/_test.pyx So I…
jramm
  • 6,415
  • 4
  • 34
  • 73
3
votes
2 answers

pythonic way of composing itemgetter and attrgetter?

I've got some code I'm porting to Cython which had a line like my_list.sort(key=lambda x: x.attr[item]) Is there a nice pythonic way of avoiding the closure with some combination of itemgetter and attrgetter?
gmoss
  • 1,019
  • 5
  • 17
3
votes
1 answer

Converting python code to cython

I have a python program that uses OpenCV. The program runs as expected as it is at the moment. Now I would like to use Cython to compile my python code to C code. I am doing this instead of re-writing the entire program in C because I would still…
Anthony
  • 33,838
  • 42
  • 169
  • 278
3
votes
2 answers

Embedding Cython Class Methods in C++

I am trying to embed a Cython class in a C++ class. Creating a Cython wrapper for this C++ class is not possible given the constraints of the project. And due to the number of methods in the Cython classes and long inheritances of the Cython…
3
votes
1 answer

Is it possible to write "pure" c++ class in Cython?

In Cython, a class, or a extension type is a Python class, which means it can be initialized by Python. On the other hand, the parameters of its __init__ or __cinit__ have to be Python Object. Is it possible to write a class in Cython, which can…
iuradz
  • 1,128
  • 1
  • 12
  • 25
3
votes
2 answers

Lambdify works with Python, but throws an exception with Cython

My website runs this Python script that would be way more optimized if Cython is used. Recently I needed to add Sympy with Lambdify, and this is not going well with Cython. So I stripped the problem to a minimum working example. In the code, I have…
The Quantum Physicist
  • 24,987
  • 19
  • 103
  • 189
3
votes
2 answers

How to run cython code in pycharm with anaconda?

I use anaconda in pycharm, and as you may know, cython has been installed by default in anaconda. My project contains a little cython code, which I want to run in pycharm. How can I do this?
scapa
  • 119
  • 1
  • 6
3
votes
1 answer

Cython: storing unicode in numpy array

I'm new to cython, and I've been having a re-ocurring problem involving encoding unicode inside of a numpy array. Here's an example of the problem: import numpy as np cimport numpy as np cpdef pass_array(np.ndarray[ndim=1,dtype=np.unicode] a): …
kazimir.r
  • 387
  • 1
  • 3
  • 9
3
votes
1 answer

Cython dictionary / map

I have a list of element, label pairs like this: [(e1, l1), (e2, l2), (e3, l1)] I have to count how many labels two element have in common - ie. in the list above e1and e3have the label l1 in common and thus 1 label in common. I have this Python…
user28906
  • 91
  • 1
  • 5
3
votes
2 answers

Cython: LINK : fatal error LNK1104: cannot open file 'atls.lib'

I'm having issues linking the atl library in my cython project. I currently have Visual C++ 9.0 and Visual Studio 2008 installed with the SP1 and Visual Studio 2015. My build is successful with python 3.5 using VS2015 when I link the libraries using…
3
votes
1 answer

Cython relative import error, even when doing absolute import

I'm having trouble in Cython (with Python 3.5) with importing between modules in a single package. The error I'm getting is SystemError: Parent module '' not loaded, cannot perform relative import, even when I'm apparently doing absolute…
Giswok
  • 559
  • 5
  • 16