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

Partial import of Numpy for Pyinstaller Executable

I am writing a code for a GUI application which works fine, however would like to bundle it into an exe/app self-contained file to make it as user-friendly as possible. As part of the code, I have written a Cython extension module which performs…
Daniele
  • 31
  • 1
3
votes
2 answers

Statically link python37.dll and vcruntime140.dll when using cython --embed

Let's say I'm "cythonizing" this test.py: import json print(json.dumps({'key': 'hello world'})) with: cython test.py --embed call "C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\vcvarsall.bat" x64 cl test.c /I C:\Python37\include /link…
Basj
  • 41,386
  • 99
  • 383
  • 673
3
votes
2 answers

How to use prange in cython?

I'm trying to solve a 2D-Ising model with Monte Carlo approach. As it is slow I used Cython to accelerate the code execution. I would like to push it even further and parallelize the Cython code. My idea is to split the 2D-lattice in two, so for any…
Angelo C
  • 33
  • 1
  • 5
3
votes
1 answer

Cython FIFO cache for function result

I need some kind of cache to store the result of a function f in Cython for future reuse. A simple FIFO cache policy that discards the least recently computed result when the cache is full will do just fine. I need the cache to be reinitialised…
Jan Joswig
  • 693
  • 5
  • 20
3
votes
1 answer

How to use C complex numbers in 'language=c++' mode?

Most of my library is written with Cython in the "normal" C mode. Up to now I rarely needed any C++ functionality, but always assumed (and sometimes did!) I could just switch to C++-mode for one module if I wanted to. So I have like 10+ modules in…
oli
  • 659
  • 1
  • 6
  • 18
3
votes
0 answers

Is my Python/Cython iteration benchmark representative?

I want to iterate through a large data structure in a Python program and perform a task for each element. For simplicity, let's say the elements are integers and the task is just an incrementation. In the end, the last incremented element is…
3
votes
1 answer

Cython min and max on Arrays

I want to speed up a quite simple Python code by converting some functions into cython. However, in the loop body, I need to find the min and max values of an array and that seems to be the critical point. According to the .html file, these lines…
3
votes
0 answers

Adding custom dllmain to cythonized module

Consider the following scenario: A python tool should be deployed for external users in a way that (a) the source is protected and (b) it is ensured that only users with valid license can use it. Furthermore, for internal users, the code should be…
3
votes
2 answers

ERROR: Could not find a version that satisfies the requirement Cython (from versions: none) ERROR: No matching distribution found for Cython

OS - Ubuntu 18.04.4 LTS; python3; kivy 1.11.1; cython - 0.29.9 (as recommended for kivy 1.11.1, but also tried the latest version at today's date) When I executed buildozer -v android debug, many errors came but were sorted out with the blessings…
3
votes
0 answers

Fast assignment to Cython memoryview

I have to assign values to a 3-dimensional array. After some benchmarking, I found that assignment of the value is much slower than computing the value to assign. Which means for i in range(n): for j in range(g): for k in…
deep_lazy
  • 31
  • 2
3
votes
1 answer

Coercion from Python not allowed without the GIL

I am trying to call a function from a C++ library defined like this: int somefunc(float timeout); I doubled a definition in the pxd file: int somefunc(float timeout) nogil; But any attempt to call the function with nogil leads to the same error…
Aleksei Petrenko
  • 6,698
  • 10
  • 53
  • 87
3
votes
2 answers

Error when trying to generate simple apk on Kivy Complete VM

Windows 10 x64 VirtualBox 6.1 Kivy Complete VM 0.7 I'm trying to generate a simple apk file and it always returns errors, I've tried to delete the virtual machine and create it again, thinking it was the result of some configuration done…
3
votes
1 answer

How to make Cython much faster than Python (without Numpy) for adding two arrays together?

I want to use Cython to decrease the time it takes to add two arrays together (element-wise) without using Numpy arrays. The basic Python approach that I found to be the fastest is to use list comprehension, as follows: def add_arrays(a,b): …
3
votes
2 answers

How can I use the Python Decimal object in cython code?

Naively I try from decimal cimport Decimal cpdef Decimal to_decimal(str value): return Decimal(value) However when I try to compile this I get the following error Error compiling Cython…
mdornfe1
  • 1,982
  • 1
  • 24
  • 42
3
votes
2 answers

Poetry + Cython + tests (Nosetests)

I use Poetry to build my package with cython extensions. Now I'd like to write tests for it (preferably with nosetest). The problem is that I need to precompile binaries what is usually done with setup.py build_clib build_ext --inplace The best…
Ivan Mishalkin
  • 1,049
  • 9
  • 25