Recently, Cython has written *. pyx extension, compiled it into a C file, and finally converted it into a so extension for python reference.
Use distutils to compile and createSetup. pyScript
From distutils. core import setup
From distutils. extension import Extension
From Cython. Distutils import build_ext
Setup (
Name = 'cython _ build_test ',
Using class = {'build _ ext ': build_ext },
Ext_modules = ext_modules
)
Parameters of the setup function:
1. name
The egg file under the site-package installation directory:/usr/lib64/python2.6/site-packages/cython_build_test-0.0.0-py2.6.egg-info. If not specified, it will be named with UNKNOW: /usr/lib64/python2.6/site-packages/UNKNOWN-0.0.0-py2.6.egg-info
2. packages tells Distutils to process those packages (including_ Init _. pyFolder)
3,Package_dirsTell Distutils which directories the files are mapped to which source package. For example, package_dir = {'': lib} indicates that all modules in" root package "are in the lib directory 4,Ext_modulesIs a list of Extension instances. The Extension definition also has some parameters, which are defined as follows:
5,Ext_packageDefine the relative path of extension
6. requires defines the dependent modules
7,ProvidesDefine which modules can provide Dependencies
8,ScriptsSpecifies the python source code file, which can be executed from the command line. Specify-install-script during installation
9,Package_dataIt usually contains some data files related to the package implementation or files similar to readme. If no template is provided, it will be added to the MANIFEST file.
10. data_files specify other files (such as configuration files)
For more information about the ext_modules parameters, see:
Define the Extension instance list,
1. name
Name of the extension package for python to import impZ plugin? Http://www.bkjia.com/kf/ware/vc/ "target =" _ blank "class =" keylink "> memory + CjxzdHJvbmc + cores/r/cores/qOsu/LIzrrOxuTL + 9PJYnVpbGRfZXh0w/zB7si3yM/cores + Cores + CjxwPjxzdHJvbmc + response/rXExL/response + PC9wPgo8cD48c3Ryb25nPjxpbWcgc3JjPQ = "" alt = "\">
5. library_dirs
Provides a list of directories in the C/C ++ Library (for example, *.)
6. libraries
A list composed of database names (not file names or paths)
Simple setup. py file:
(1)
Run setup. py and pass the build_ext parameter. Option-inplace
Python
Setup. py
Build_ext-inplace
System report:
Running build_ext
Cythoning import_cython_c_math.pyx to import_cython_c_math.c
Building "import_cython_c_math 'extension
Creating build
Creating build/temp. linux-x86_64-2.6
Gcc-pthread-fno-strict-aliasing-O2-g-pipe-Wall-Wp, -D_FORTIFY_SOURCE = 2-fexceptions-fstack-protector -- param = ssp-buffer-size = 4-m64-mtune = generic-D_GNU_SOURCE-fPIC-fwrapv-DNDEBUG-O2-g-pipe -Wall-Wp, -D_FORTIFY_SOURCE = 2-fexceptions-fstack-protector -- param = ssp-buffer-size = 4-m64-mtune = generic-D_GNU_SOURCE-fPIC-fwrapv-fPIC-I/usr/include /python2.6-c import_cython_c_math.c-obuild/temp. linux-x86_64-2.6/import_cython_c_math.o
Gcc-pthread-shared build/temp. linux-x86_64-2.6/import_cython_c_math.o-L/usr/lib64-lpython2.6-o/home/linshaowei/cython_setup_test/import_cython_c_math.so
At this point:
Automatically created in the current directory:
Build directory
Go to the build Directory, which has a directory: temp. linux-x86_64-2.6, the directory contains the import_cython_c_math.o file, the file is the c Code Compiled after the object file containing the 0, 1 command (that is, the c language compiled file)
Import_cython_c_math.so, followed by generating a dynamic link library directly referenced by python, which is a link file
Now you can reference the library file in python.
(2)
Run setup. py and pass the build parameter (compile)
Python
Setup. py
Build
System report:
Running build
Running build_ext
Cythoning import_cython_c_math.pyx to import_cython_c_math.c
Building 'import _ cython_c_math' extension
Creating build
Creating build/temp. linux-x86_64-2.6
Gcc-pthread-fno-strict-aliasing-O2-g-pipe-Wall-Wp, -D_FORTIFY_SOURCE = 2-fexceptions-fstack-protector -- param = ssp-buffer-size = 4-m64-mtune = generic-D_GNU_SOURCE-fPIC-fwrapv-DNDEBUG-O2-g-pipe -Wall-Wp, -D_FORTIFY_SOURCE = 2-fexceptions-fstack-protector -- param = ssp-buffer-size = 4-m64-mtune = generic-D_GNU_SOURCE-fPIC-fwrapv-fPIC-I/usr/include /python2.6-c import_cython_c_math.c-o build/temp. linux-x86_64-2.6/import_cython_c_math.o
Creating build/lib. linux-x86_64-2.6
Gcc-pthread-shared build/temp. linux-x86_64-2.6/import_cython_c_math.o-L/usr/lib64-lpython2.6-o build/lib. linux-x86_64-2.6/import_cython_c_math.so
At this time,
Automatically created in the current directory:
Build directory
Go to the build Directory, which has two directories:
Lib. linux-x86_64-2.6 stored in this directory:
Import_cython_c_math.soFile
Temp. linux-x86_64-2.6 stored in this directory: import_cython_c_math.o File
Sudo python
Setup. py
Install)
Running install
Running build
Running build_ext
Skipping 'import _ cython_c_math.c 'Cython extension (up-to-date)
Running install_lib
Copying build/lib. linux-x86_64-2.6/import_cython_c_math.so->/usr/lib64/python2.6/site-packages
Running install_egg_info
Writing/usr/lib64/python2.6/site-packages/cython_build_test-0.0.0-py2.6.egg-info
InstallThe Dynamic Link file build/lib. linux-x86_64-2.6/import_cython_c_math.so creates the soft link/usr/lib64/python2.6/site-packages
Write Writing/usr/lib64/python2.6/site-packages/cython_build_test-0.0.0-py2.6.egg-info python installation directory, generate the egg file and put it in the installation directory, it means that python can reference it everywhere