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
1 answer

How do I add pkg-config the setup.py of a Cython wrapper

How can I add pkg-config to the following setup.py (for example if I want to add glib-2.0) from distutils.core import setup from distutils.extension import Extension from Cython.Build import cythonize wrapper = Extension( name="wrapper", …
user10207893
  • 243
  • 1
  • 2
  • 11
3
votes
2 answers

dill.dump_session won't work in jupyter lab

I want to start working on jupyter lab instead of spyder but i have a problem. I can't save the variables in my workspace. I am trying to use jupyter lab to run a code like this: import dill import pandas as pd import numpy as np df =…
3
votes
2 answers

Going faster than struct.pack with cython

I'm trying to do better than struct.pack. Taking a specific case of packing integeres, via the answer to this question, I have the following to pack a list of ints in pack_ints.pyx: # cython: language_level=3, boundscheck=False import…
Jay
  • 2,535
  • 3
  • 32
  • 44
3
votes
1 answer

Reloading Cython modules on the fly

I am trying to auto update Cython .so modules that my python program uses on the fly. After I download the new module and del module and import module Python seems to still be importing the older version. From this question, I've tried this but it …
Vinayak
  • 1,103
  • 3
  • 18
  • 40
3
votes
1 answer

How to use memcpy in Cython

with open(fname, 'rb') as fp: line = fp.readline().strip() content = fp.read() cdef int nb = len(content) #print("Hello ", nb) cdef char* c_string = malloc((nb + 1) * sizeof(char)) cdef char* tmp_str =
JeffQ
  • 94
  • 5
3
votes
1 answer

A Priority queue with a custom comparator in Cython

I know there is already this question answered but I can't seem to make it work. I'm currently trying to use a PriorityQueue of pair[double,pair[int,int]] and use the double of the pair (pair.first) to sort itself. Incase this helps, the method is…
GSR
  • 49
  • 3
3
votes
1 answer

Why dgemm (Cython compiled) is slower than numpy.dot

So long story short, I built a simple multiplication function in Cython, invoking scipy.linalg.cython_blas.dgemm, compile it and run it against the benchmark Numpy.dot. I have heard the myth about the 50% to 100x performance gain I will witness when…
fnosdy
  • 123
  • 8
3
votes
4 answers

How to install C++ 14.0 Offline and make python aware of it?

I am trying to figure out how to install c++ 14.0 offline using suggestions from other Stackoverflow Q&A. But none of them seem to work. I need it for Cython. I don't want to install the binary version. I installed suggested redistributables and…
Marco Fernandes
  • 326
  • 1
  • 4
  • 13
3
votes
2 answers

How to store a boolean mask as an attribute of a Cython class?

I failed to save a boolean mask as an attribute of a Cython class. In the real code I need this mask to perform tasks more efficiently. Here it follows a sample code: core.pyx import numpy as np cimport numpy as np cdef class MyClass(): cdef…
Saullo G. P. Castro
  • 56,802
  • 26
  • 179
  • 234
3
votes
1 answer

How to fix "cannot find -lvcruntime140.dll" while compiling .py to .pyd?

I am trying to convert my python module from .py to .pyd dll. Every time I tried to excute my setup script. from distutils.core import setup from distutils.extension import Extension from Cython.Distutils import build_ext ext_modules = [ …
3
votes
1 answer

How to convert Python object to a std::vector of Cython extension type and back?

I use Cython to wrap C++ code. The code contains a function defined as: std::vector analyze(std::vector inputVec); ClassIn and ClassOut are extension types. From Python I'd like to be able to call this function with a list or a…
zyzzler
  • 105
  • 7
3
votes
1 answer

How to use an object of a wrapped class as an argument in a wrapped function

I use cython to wrap C++ code and make it available in Python. The problem I'm facing is that I want to use a wrapped class as an argument in a function that I want to wrap as well. So from Python's point of view I want to create and modify an…
zyzzler
  • 105
  • 7
3
votes
1 answer

Poetry + Sphinx + Cython

I use poetry to build my cython package. I have NumPy-style docstrings in all functions and classes. What I want to do now is to add Sphinx automatic documentation and publishing it at Read the Docs. I have read this thread How do I use Sphinx with…
Ivan Mishalkin
  • 1,049
  • 9
  • 25
3
votes
1 answer

cython openmp single, barrier

I'm trying to use openmp in cython. I need to do two things in cython: i) use the #pragma omp single{} scope in my cython code. ii) use the #pragma omp barrier{} Does anyone know how to do this in cython? Here are more details. I have a nogil…
python_freak
  • 259
  • 3
  • 12
3
votes
1 answer

Cython vs Python benchmark

I'm trying to get a benchmark from cython vs pure python with bubble sort algorithm, and this is my result, that shows me pure python is faster than cython is my result correct or something is wrong with my code? The script tries to create a random…
Rasool Ziafaty
  • 180
  • 3
  • 13