cython

cython是python的一个模块,Cython可以把代码编译成C模块

 

1、需要用Cython把.pyx文件编译(翻译)成.c文件。这些文件里的源代码,基本都是纯Python代码加上一些Cython的代码。
2、然后,.c文件被C语言编译器编译成.so库,这个库之后可以导入python。
3、编译代码有一些方法:
- 我们可以创建一个distutils配置文件。bistutils是一个创建其他模块的工具,我们可以用它生成自定义的C语言编译文件
- 运行cython命令将.pyx文件编译成.c文件。然后用C语言编译器把C代码手动编译成库文件
- 最后一种是用pyximport,像导入.py一样导入.pyx直接使用

 

linux环境下
# test.pyx
def join_n_print(parts): print(
' '.join(parts))
# setup.py
from distutils.core import setup from Cython.Build import cythonize setup( name='tset app', ext_modules=cythonize('test.pyx') )
python3 setup.py build 
. ├── build │   ├── lib.linux
-x86_64-3.5 │   │   └── test.cpython-35m-x86_64-linux-gnu.so │   └── temp.linux-x86_64-3.5 │   └── test.o ├── setup.py ├── test.c └── test.pyx

└── test.cpython-35m-x86_64-linux-gnu.so
把这个文件放到这里
在python环境里就可以直接import test导入了
注意.pyx是导入不了的
.
├── build
│   └── temp.linux-x86_64-3.5
│       └── hello.o
├── hello.c
├── hello.cpython-35m-x86_64-linux-gnu.so
├── hello.pyx
└── setup.py
build_ext --inplace
 window环境下
python3 setup.py build 
.
|-- build
|   |-- lib.win32-3.6
|   |   `-- compute.cp36-win32.pyd
|   `-- temp.win32-3.6
|       `-- Release
|           |-- compute.cp36-win32.exp
|           |-- compute.cp36-win32.lib
|           `-- compute.obj
|-- compute.c
|-- compute.pyx
`-- setup.py
使用方法和上面一样
build_ext   --inplace
$ tree .
.
|-- build
|   `-- temp.win32-3.6
|       `-- Release
|           |-- compute.cp36-win32.exp
|           |-- compute.cp36-win32.lib
|           `-- compute.obj
|-- compute.c
|-- compute.cp36-win32.pyd
|-- compute.pyx
`-- setup.py
build_ext --inplace
问题
python3 setup.py build_ext命令   在setup.py按照上面写的时候目录结构一样
发现pyd文件 或者对应的so文件,路径都有些问题,不能直接导入。

方法: python3 setup.py build_ext --inplace
$ python setup.py build --help
Common commands: (see '--help-commands' for more)
setup.py build will build the package underneath
'build/' setup.py install will install the package Global options: --verbose (-v) run verbosely (default) --quiet (-q) run quietly (turns verbosity off) --dry-run (-n) don't actually do anything --help (-h) show detailed help message --no-user-cfg ignore pydistutils.cfg in your home directory Options for 'build' command: --build-base (-b) base directory for build library --build-purelib build directory for platform-neutral distributions --build-platlib build directory for platform-specific distributions --build-lib build directory for all distribution (defaults to either build-purelib or build-platlib --build-scripts build directory for scripts --build-temp (-t) temporary build directory --plat-name (-p) platform name to build for, if supported (default: win32) --compiler (-c) specify the compiler type --parallel (-j) number of parallel build jobs --debug (-g) compile extensions and libraries with debugging information --force (-f) forcibly build everything (ignore file timestamps) --executable (-e) specify final destination interpreter path (build.py) --help-compiler list available compilers usage: setup.py [global_opts] cmd1 [cmd1_opts] [cmd2 [cmd2_opts] ...] or: setup.py --help [cmd1 cmd2 ...] or: setup.py --help-commands or: setup.py cmd --help
$ python setup.py build_ext --help
Common commands: (see '--help-commands' for more)

  setup.py build      will build the package underneath 'build/'
  setup.py install    will install the package

Global options:
  --verbose (-v)  run verbosely (default)
  --quiet (-q)    run quietly (turns verbosity off)
  --dry-run (-n)  don't actually do anything
  --help (-h)     show detailed help message
  --no-user-cfg   ignore pydistutils.cfg in your home directory

Options for 'old_build_ext' command:
  --build-lib (-b)           directory for compiled extension modules
  --build-temp (-t)          directory for temporary files (build by-products)
  --plat-name (-p)           platform name to cross-compile for, if supported
                             (default: win32)
  --inplace (-i)             ignore build-lib and put compiled extensions into
                             the source directory alongside your pure Python
                             modules
  --include-dirs (-I)        list of directories to search for header files
                             (separated by ';')
  --define (-D)              C preprocessor macros to define
  --undef (-U)               C preprocessor macros to undefine
  --libraries (-l)           external C libraries to link with
  --library-dirs (-L)        directories to search for external C libraries
                             (separated by ';')
  --rpath (-R)               directories to search for shared C libraries at
                             runtime
  --link-objects (-O)        extra explicit link objects to include in the
                             link
  --debug (-g)               compile/link with debugging information
  --force (-f)               forcibly build everything (ignore file
                             timestamps)
  --compiler (-c)            specify the compiler type
  --parallel (-j)            number of parallel build jobs
  --swig-cpp                 make SWIG create C++ files (default is C)
  --swig-opts                list of SWIG command line options
  --swig                     path to the SWIG executable
  --user                     add user include, library and rpath
  --cython-cplus             generate C++ source files
  --cython-create-listing    write errors to a listing file
  --cython-line-directives   emit source line directives
  --cython-include-dirs      path to the Cython include files (separated by
                             ';')
  --cython-c-in-temp         put generated C files in temp directory
  --cython-gen-pxi           generate .pxi file for public declarations
  --cython-directives        compiler directive overrides
  --cython-gdb               generate debug information for cygdb
  --cython-compile-time-env  cython compile time environment
  --pyrex-cplus              generate C++ source files
  --pyrex-create-listing     write errors to a listing file
  --pyrex-line-directives    emit source line directives
  --pyrex-include-dirs       path to the Cython include files (separated by
                             ';')
  --pyrex-c-in-temp          put generated C files in temp directory
  --pyrex-gen-pxi            generate .pxi file for public declarations
  --pyrex-directives         compiler directive overrides
  --pyrex-gdb                generate debug information for cygdb
  --help-compiler            list available compilers

usage: setup.py [global_opts] cmd1 [cmd1_opts] [cmd2 [cmd2_opts] ...]
   or: setup.py --help [cmd1 cmd2 ...]
   or: setup.py --help-commands
   or: setup.py cmd --help

 

setup.py的另一种写法

from distutils.core import setup
from distutils.extension import Extension
from Cython.Distutils import build_ext
ext_modules = [Extension("compute",["compute.pyx"])]
setup(
    name = "compute pyx",
    cmdclass = {'build_ext': build_ext},
    ext_modules = ext_modules
)

 

posted @ 2018-12-29 16:32  慕沁  阅读(1616)  评论(0)    收藏  举报