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

How to unitest with cython from .py file

I tried to search for this problem on Google but fail. I have a .pyx file constant a cython variable. Now I want to mock this variable for the test. What are the best ways to do that? For example: .pyx: cdef dict example { A: B, C: D } I…
Trần Đức Tâm
  • 4,037
  • 3
  • 30
  • 58
3
votes
1 answer

Cython convert string to unicode

I am trying to use this lib https://github.com/pytries/datrie to manipulate Chinese text . But I encounter a problem - it has problem to encode decode Chinese unicode: import datrie text = htmls_2_text(input_dir) trie =…
Mithril
  • 12,947
  • 18
  • 102
  • 153
3
votes
0 answers

Creating a cython file: No such file or directory

I have recently switched to a windows OS but file is not being found with cython. The file seems correct. I run: CD C:\Users\Django\example-of-cython-pyinstaller-and-subzero\1 ACTUAL PROJECT `python setup.py build_ext --inplace` and get `No such…
user9088873
3
votes
1 answer

How to build in Cython an iostream object (e. g. cout) and pass it to a c++ function?

I'm trying to wrap the following C++ class: print.h #include using namespace std; class ControlParameters { public: ControlParameters(); ~ControlParameters(); void Initialize(string input, ostream& log); }; print.cpp #include…
Piet P.
  • 33
  • 5
3
votes
1 answer

__cinit__() takes exactly 2 positional arguments when extending a Cython class

I want to extend scikit-learn's ClassificationCriterion class which is defined as a Cython class in an internal module sklearn.tree._criterion. I would like to do that in Python, as normally I don't have access to the pyx/pxd files of sklearn (so I…
user1544337
3
votes
1 answer

What are my options to speed up and reduce the size of my kivy android application

I am wondering what options I have to speed up kivy after I package it. I was looking into cython and pypy because those should speed up python. Also, I was reading that kivy can be a little slower and bigger than most android applications because…
3
votes
4 answers

Compiling Executable with dask or joblib multiprocessing with cython results in errors

I'm converting some serial processed python jobs to multiprocessing with dask or joblib. Sadly I need to work on windows. When running from within IPython or from command line invoking the py-file with python everything is running fine. When…
Bastian Ebeling
  • 1,138
  • 11
  • 38
3
votes
1 answer

Cython No Performance Increase with prange/parallel

I'm using Cython version 0.27.3 to compile the following source for a simple primality testing module that contains both python and cython implementations of the same algorithm. When I set the threads parameter to different values, I see no…
Goodies
  • 4,439
  • 3
  • 31
  • 57
3
votes
0 answers

Slowing down numpy code with cython

I'm trying to accelerate an algorithm I have with cython. I've followed most of the proscriptions on this page to take my original function and convert it into an optimized form, but all I appear to have done is slowed things down. I've checked the…
Vyas
  • 234
  • 1
  • 10
3
votes
1 answer

Calling Cython from c++ with not built-in types

I'm trying to extend functionality of my lib written in C++ with Python and Cython. I have class MyClass in C++ which is essential for my lib. And I'm using a lot it's wrapper class PyMyClass in Python. So I want to use functions (with PyMyClass as…
Zhu
  • 57
  • 8
3
votes
0 answers

Error: distutils.errors.DistutilsExecError: command 'gcc' failed with exit status 1 running cython

I'm trying to call a cython file .pyx from a python file .py but I find the error: fatal error: 'numpy/arrayobject.h' file not found #include "numpy/arrayobject.h" followed by: distutils.errors.CompileError: command 'gcc' failed with exit status…
3
votes
1 answer

Enabling Parallelism with Cython

I am trying to get the prange function of Cython's parallel package to work and it seems like there is no parallelism in effect. To have a MWE, I have taken the example code from the book Cython: A Guide for Python Programmers and modified it a…
Pavithran Iyer
  • 402
  • 5
  • 14
3
votes
1 answer

How to expose static constexpr from cython

I need to wrap the following demo example from cpp to cython for using in Python class Foo1 : public MFoo { public: constexpr Foo1(unsigned int val) : MFoo{val} {} } }; class Foo{ public: static constexpr Foo1 any {0}; …
Chinmoy Kulkarni
  • 187
  • 1
  • 13
3
votes
1 answer

Set visibility in pure-python Cython code

one of my major problems with cython cdef classes is that all their variables are by default private. When I am converting some python code to pure-python-mode cython, I have to manually use var_name = cython.declare(type, visibility='public) for…
Lefty
  • 407
  • 5
  • 12
3
votes
1 answer

Cython: creating an array throws "not allowed in a constant expression"

I try to rewrite a complex function form Python to Cython to speed it up massively and I encounter the following problem: while compiling my function hh_vers_vector.pyx using setup( ext_modules=cythonize("hh_vers_vector.pyx"), ) it throws the…
Svenno Nito
  • 635
  • 1
  • 6
  • 22