Pyx file generation PYD file for Cython call

Source: Internet
Author: User
Tags python script

Reference: http://www.2cto.com/kf/201405/304168.html

Reference: http://blog.csdn.net/darren2015zdc/article/details/54574868


The Pyx file is a Python C extension file that conforms to the Cython specification and writes with whatever editor. I wrote it on the Eric4, and as a result it was interpreted by default with the Python interpreter and prompted with bugs, "syntax errors."

The above Pyx file is only a source code file, to be called by Python, to run, just write the source code is not enough. Specifically, you will also want to convert to. C or. C + + files, and then go further into the. pyd file. PYD files are files that can be used directly. To achieve this, write a setup.py script, as follows:

#!/usr/bin/python
#python version:2.7.3
#Filename: setuptestomp.py
 
# Run as:  
#    python setup.py Build_ext--inplace  
   
Import sys  
sys.path.insert (0, "..")  
   
From Distutils.core Import Setup from  
distutils.extension import extension to  
cython.build import cythonize< C10/>from cython.distutils Import build_ext
   
# ext_module = cythonize ("Testomp.pyx")  
Ext_module = Extension ( c13/> "Testomp",
            ["Testomp.pyx"],
            extra_compile_args=["/openmp"],
            extra_link_args=["/openmp"],
            )
   
Setup (
    cmdclass = {' Build_ext ': Build_ext},
        ext_modules = [Ext_module], 
)
This is a completely Python script that can be run under the Python interpreter. Under the console, run the following command ' python setup.py build_ext--inplace ' to generate testomp.pyd files. Of course, there are also some assorted files, such as the ' Lib ' file under the ' Build ' directory. This is prompted by the Windows Vistual Studio environment. In the LINUX+GCC environment, it is necessary to generate the. so file, and the "/openmp" option will be written "-fopenmp"

Write testomp.py

The two steps in the file are equivalent to writing Python's C extension form in more efficient code for a Python efficiency bottleneck module (which needs to be positioned with profile tools), and then to call them in Python code. Testomp.py is the script for this call, as follows:

From Testomp Import Test 
test ()
This is easy, import and call. Under the console, type "Python testomp.py" to run.




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.