Python and c| C + + (ii): Using Cython for mixed

Source: Internet
Author: User

You can also use Cython to implement

1 Download Cython, install with Python setup.py installation

21 instances

① Creating the HelloWorld directory

Create a Helloworld.pyx with the following content:

cdef extern from"Stdio.h":

extern int printf (const char *format, ...)

def SayHello ():

printf ("hello,world\n")

② compiled, the most convenient is the use of Python distutils,

Create setup.py under the HelloWorld directory, as follows:

from Distutils.core import Setup

from distutils.extension import Extension

from Cython.build Import cythonize

Setup

Name = 'helloworld',

Ext_modules=cythonize ([

Extension ("HelloWorld", ["Helloworld.pyx"]),

]),

)

Compile:

Python setup.py Build

Installation:

Python setup.py Install

After installation, it will be in Build/lib.??? Helloworld.pyd copies generated in the directory to Lib/site-packages

Note:

Sometimes we just want to test, do not want to install, then we can put build/lib.??? Helloworld.pyd copy of the directory to the current directory

or execute the script before Importhelloworld: Import sys;sys.path.append (Pathof helloworld.pyd)

③ Test:

>>>import HelloWorld

>>>helloworld. SayHello ()

Hello,world

Python and c| C + + (ii): Using Cython for mixed

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.