Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

using cython to port modules to python 3.1

Is it possible to import arbitrary modules in cython, compile them to shared object files and then use them in python 3.1?

The reason for this is, that I am writing an extension for the program "blender", which has an internal python 3.1 interpreter. But i would also like to make use of some python-modules which are not ported to 3.x, yet

I have specifically numpy in my mind (but also some other libraries). I have a module, which makes use of numpy. As I want to redistribute that module, I don't want poeple to install numpy on their machines. would that work?

like image 699
user290494 Avatar asked Nov 28 '10 14:11

user290494


People also ask

Do Python libraries work with Cython?

Cython Hello World As Cython can accept almost any valid python source file, one of the hardest things in getting started is just figuring out how to compile your extension.

How do you use Cython in Python?

To make your Python into Cython, first you need to create a file with the . pyx extension rather than the . py extension. Inside this file, you can start by writing regular Python code (note that there are some limitations in the Python code accepted by Cython, as clarified in the Cython docs).

Is Cython as fast as C?

Cython code runs fastest when “pure C” But if that function references any Python-native code, like a Python data structure or a call to an internal Python API, that call will be a performance bottleneck.


1 Answers

In principle, I believe it's possible. Cython works by translating Python-like code to C code. That code can be compiled for either Python 2 or Python 3 (it uses C preprocessor statements to change which code is used).

The bad news is that it will only work for extensions written in Python-like code that Cython can translate. You can't use Cython on extensions written in C, like Numpy.

The good news is that, at least for Numpy, you shouldn't have to. Since version 1.5, Numpy supports Python 3. There's a binary available for Windows; on other systems, you might have to work out how to compile the code yourself.

like image 162
Thomas K Avatar answered Oct 13 '22 14:10

Thomas K