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

Why does numpy.zeros takes up little space

I am wondering why numpy.zeros takes up such little space? x = numpy.zeros(200000000) This takes up no memory while, x = numpy.repeat(0,200000000) takes up around 1.5GB. Does numpy.zeros create an array of empty pointers? If so, is there a way to…
user3266890
  • 465
  • 5
  • 15
13
votes
3 answers

pip error: unrecognized command line option ‘-fstack-protector-strong’

When I sudo pip install pyquery, sudo pip install lxml, and sudo pip install cython, I get very similar output with the same error that says: x86_64-linux-gnu-gcc: error: unrecognized command line option ‘-fstack-protector-strong’ Here's the…
cooltoast
  • 297
  • 2
  • 3
  • 8
13
votes
2 answers

LNK1181: cannot open input file 'm.lib'

When trying to install a certain Python geophysical toolkit, I get this error: LINK : fatal error LNK1181: cannot open input file 'm.lib' I believe it is due to my use of the MSVC's buildtools. In their setup.py I found: setup(…, ext_modules=[…
A T
  • 13,008
  • 21
  • 97
  • 158
13
votes
1 answer

Passing a numpy pointer (dtype=np.bool) to C++

I'd like to use a numpy array of type bool in C++ by passing its pointer via Cython. I already know how to do it with other datatypes like uint8. Doing it the same way with boolean it does not work. I am able to compile but there is the following…
lilei123
  • 175
  • 1
  • 6
13
votes
2 answers

Fast string array - Cython

Having following hypothetical code: cdef extern from "string.h": int strcmp(char* str1, char* str2) def foo(list_str1, list_str2): cdef unsigned int i, j c_arr1 = ?? c_arr2 = ?? for i in xrange(len(list_str1)): for j in…
Jendas
  • 3,359
  • 3
  • 27
  • 55
13
votes
3 answers

How to represent inf or -inf in Cython with numpy?

I am building an array with cython element by element. I'd like to store the constant np.inf (or -1 * np.inf) in some entries. However, this will require the overhead of going back into Python to look up inf. Is there a libc.math equivalent of this…
user248237
13
votes
1 answer

Pandas installation on Mac OS X: ImportError (cannot import name hashtable)

I would like to build pandas from source rather than use a package manager because I am interested in contributing. The first time I tried to build pandas, these were the steps I took: 1) created the virtualenv mkvirtualenv --no-site-packages…
Emily Chen
  • 247
  • 1
  • 3
  • 8
13
votes
3 answers

Cython and C++ inheritance

I have 2 classes, A and B. B inherits from A. //C++ class A { public: int getA() {return this->a;}; A() {this->a = 42;} private: int a; }; class B: public A { public: B() {this->b = 111;}; int…
ascobol
  • 7,554
  • 7
  • 49
  • 70
12
votes
3 answers

Gensim install in Python 3.11 fails because of missing longintrepr.h file

Operating System: macOS Monterey 12.6 Chip: Apple M1 Python version: 3.11.1 I try: pip3 install gensim The install process starts well but fatally fails towards the end while running 'clang'. The error message is: clang -Wsign-compare…
Halim Gurgenci
  • 135
  • 1
  • 1
  • 5
12
votes
3 answers

How to use annotate=True on Cythonize()

I'm new to Cython, but got it working by following this basic guide from the official docs: All it says is: "Cython has a way to visualise where interaction with Python objects and Python’s C-API is taking place. For this, pass the annotate=True…
Bruce Nielson
  • 753
  • 8
  • 23
12
votes
3 answers

Using self-defined Cython code from other Cython code

I am currently trying to optimize my Python program and got started with Cython in order to reduce the function calling overhead and perhaps later on include optimized C-libraries functions. So I ran into the first problem: I am using composition in…
packoman
  • 1,230
  • 1
  • 16
  • 36
12
votes
1 answer

Compile Python code to statically linked executable with Cython

I have a pure Python script that I would like to distribute to systems with unkown Python configuration. Therefore, I would like to compile the Python code to a stand-alone executable. I run cython --embed ./foo.py without problems giving foo.c.…
phlegmax
  • 419
  • 1
  • 4
  • 11
12
votes
2 answers

Cythonize a Python function to make it faster

Few weeks ago I asked a question on increasing the speed of a function written in Python. At that time, TryPyPy brought to my attention the possibility of using Cython for doing so. He also kindly gave an example of how I could Cythonize that code…
Curious2learn
  • 31,692
  • 43
  • 108
  • 125
12
votes
1 answer

Cython: Compile a Standalone Static Executable

I'm trying to compile an executable (ELF file) that does not use a dynamic loader. I used Cython to compile Python to C: cython3 -3 test.py --embed Then gcc test.c -otest $(pkg-config --libs --cflags python3) to compile C generated file. Now I'd…
GinoC
  • 442
  • 4
  • 16
12
votes
3 answers

Real-time audio signal processing using python

I have been trying to do real-time audio signal processing using 'pyAudio' module in python. What I did was a simple case of reading audio data from microphone and play it via headphones. I tried with the following code(both Python and Cython…
Sajil C K
  • 171
  • 1
  • 3
  • 13