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

Source: Internet
Author: User

can also use Cython to achieve mixed

1 Download Cython. installing with Python setup.py install

21 instances

① Creating the HelloWorld folder

Create Helloworld.pyx, such as the following:

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,

Under the HelloWorld folder, create the setup.py, such as the following:

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. will be in Build/lib.??

? Helloworld.pyd copied to lib/site-packages under folder

Note:

Sometimes we just want to test it. Do not want to install. At this time can put build/lib.??? Folder Helloworld.pyd copied to the current folder

or run 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

Related Article

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.