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

Cython can cast int 65 into char 'A'?

%%cython import cython cdef int k = 65 cdef unsigned char kk = cython.cast("char", k) print kk the result is 65. I already tried to convert 65 to 'A' Anyone have some ideas? I am currently working in Ipython notebook. Thank you in…
3
votes
1 answer

Python package Cython module

I have the following package structure: + repo/ + setup.py + package/ + module1/ + submodule1.py + submodule2.pyx + module2/ + submodule3.py I would like to use submodule2.pyx from submodule1.py by something…
Eric
  • 946
  • 2
  • 12
  • 25
3
votes
2 answers

How to use cython_freeze on Windows to build a single executable file?

I am running Python 3.5 on Windows 10 and I would like to compile my Python code into a single executable to share with some end users. I'm using Cython 0.25.2 to try to get this done. I've got a HelloWorld program working, by using the Cython…
Casey
  • 475
  • 6
  • 19
3
votes
0 answers

How to define in Cython an external C++ function which has input parameters as enums? MKL

So trying to wrap this in Cython from MKL, and digging in the mkl_cblas.h I see these are enums: typedef enum {CblasRowMajor=101, CblasColMajor=102} CBLAS_ORDER; typedef enum {CblasNoTrans=111, CblasTrans=112, CblasConjTrans=113}…
Matt
  • 2,602
  • 13
  • 36
3
votes
1 answer

Cython : importing a .pyd file returns an error (lack of an init function ?)

I am learning how to use Cython to compile efficiently a Python code and make it quicker. Here is what I've done so far: I created a Python file called math_code_python.py and put 4 simple functions in it. I saved that file as…
Loïc Poncin
  • 511
  • 1
  • 11
  • 30
3
votes
3 answers

Does Cython support the ternary style if statement (if ? then : else)?

I forget the technical term for the (if ? then : else) format in C/C++. Also, is this syntax supported in the Cython (a C variant of Python)? I need to know the technical name so I can lookup if Cython supports this feature. UPDATE: Does anyone know…
unixman83
  • 9,421
  • 10
  • 68
  • 102
3
votes
2 answers

how to prepare cython submodules

I have three questions, but related, and I not getting how can I split them well. I've found many information about those issues, like submodule extension, hierarchy, about an empty __init__.py file, and how to cythonize multiple pyx files. But…
srgblnch
  • 323
  • 3
  • 11
3
votes
0 answers

How to include multiple headers in Cython

I'll start off by saying I don't really know C++ or cython but I need to write a python wrapper for some c++ code. I may be going about this the wrong way but from what I understand you include a header file in cython with cdef extern from…
user5702166
3
votes
1 answer

Defining a C++ struct return type in Cython

I have written a small C++ library to simulate a system of ODEs, and have used Cython to wrap a function that initializes and solves the system. The C++ function takes in a handful of parameters, and returns a struct containing two matrices: a set…
Max Gillett
  • 177
  • 3
  • 14
3
votes
0 answers

Change compiler path of Cython

I'm writing a simple example to test Cython: helloworld.pyx def foo(): print "hello cython world" test_cython.py import pyximport pyximport.install() import helloworld helloworld.foo() Which then returns: ImportError: Building module testy…
Roy Iacob
  • 412
  • 3
  • 13
3
votes
0 answers

Do not include Cython metadata in generated C files

When generating C files from Cython code, the cython compiler adds metadata about the used version and the absolute file paths of the original Cython files. This looks something like this: /* BEGIN: Cython Metadata { "distutils": { …
m00am
  • 5,910
  • 11
  • 53
  • 69
3
votes
1 answer

Instantiating a C++ struct derived from a class in cython

I am trying to interface Python to the Bullet Physics C++ library using Cython. I would like to do this at C speeds, or near C speeds with minimal variable packing and unpacking from Python. I was able to use this approach to create a Cython…
rpdrewes
  • 147
  • 11
3
votes
1 answer

Using float literals instead of double in Cython code?

When compiling the Cython code below with MSVC: cpdef float interpolate(float start, float end, float alpha): return end * alpha + start * (1.0 - alpha) I get this warning: warning C4244: '=': conversion from 'double' to 'float', possible loss…
HankMoody
  • 3,077
  • 1
  • 17
  • 38
3
votes
0 answers

How to use std::function in cython

I've seen here that you can do from libcpp.string cimport string from libcpp.vector cimport vector to refer to parameters of type std::string and std::vector. I need to pass functions to a c++ method expecting std::function<>. I tried doing from…
Stephen
  • 2,613
  • 1
  • 24
  • 42
3
votes
2 answers

Possible optimization in cython: numpy array

The following is my Cython code for drawing from multivariate normal distribution. I am using loop because each time I have different density. (conLSigma is the Cholesky factor) This is taking a lot of time because I am taking inverse and Cholesky…
joon
  • 3,899
  • 1
  • 40
  • 53