Share Cython installation and usage experience

Y2J
Release: 2017-04-17 17:05:47
Original
4786 people have browsed it

1. What is Cython?

It is a tool used to quickly generate Python extension module (extension module) tool

its syntax is a mixture of python language syntax and c language syntax

him It is easier to write python extension modules than swig

Maybe you will say that swig can generate extension modules directly through c header files, but swig's support for callback functions is not very good,

In addition, if you use swig, in many cases, you have to write additional code to convert the input parameters into python objects and convert the output into python objects, for example, if you encapsulate a C function If the parameters are input and output, and if there is a callback function in the parameters of the C function,

and in Cython, the types in C, such as int, float, long, char*, etc. will all Automatically convert to python objects when necessary, or convert from python objects to C types. When the conversion fails, an exception will be thrown . This is the most amazing thing about Cython

In addition, Cython also has good support for callback functions.

In short, if you have the need to write python extension modules, then Cython is really a good tool

2. Convert to cython

cython Installation under linux:

1. Source package installation:

[blueelwang@pythontab ~]$ wget https://pypi.python.org/packages/b7/67/7e2a817f9e9c773ee3995c1e15204f5d01c8da71882016cac10342ef031b/Cython-0.25.2.tar.gz
[blueelwang@pythontab ~]$ tar xzvf Cython-0.25.2.tar.gz
[blueelwang@pythontab ~]$ cd Cython-0.25.2
[blueelwang@pythontab ~]$ python setup.py install
Copy after login

2. pip package installation

[blueelwang@pythontab ~]$ sudo pip install Cython --install-option="--no-cython-compile"
Copy after login

3. Installation under Ubuntu

[blueelwang@pythontab ~]$ sudo apt-get install cython
Copy after login

After installation, enter cython to verify whether the installation is successful

3. Use

1. Write with .pyx as extension cython program, hello.pyx

def say_hello_to(name):
    print("Hello %s!" % name)
Copy after login

2. Write the python program setup.py

The purpose is to convert the hello.pyx program into hello.c and compile it into an so file

from distutils.core import setup
from distutils.extension import Extension
from Cython.Distutils import build_ext
ext_modules = [Extension("hello", ["hello.pyx"])]
setup(
  name = 'Hello world app',
  cmdclass = {'build_ext': build_ext},
  ext_modules = ext_modules
)
Copy after login

3. Execute the python program

[blueelwang@pythontab ~]$ python setup.py build_ext --inplace
Copy after login

The execution result will generate two files: hello.c and hello.so (files encapsulated with PyObject*)

4. Use python to call hello.so, and the calling file is test.py

import hello
hello.say_hello_to("hi,cython!!")
Copy after login

The main purpose of cython is to: Simplify the cumbersome encapsulation process of python calling c language programs, and improve the execution speed of python code (C The execution speed of the language is faster than python)

The above is the detailed content of Share Cython installation and usage experience. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!