1. Cython安装, 比较简单:
pip install Cython
例1. 写第一个程序- helloworld.pyx
1). 新建一个helloworld.pyx文件,如下所示:
# cython: language_level=3
print("Hello world!")
# cython: language_level=3这个比较关键,不加会出现如下错误:
2).新建setup.py文件,并添入下述code,并保存。
from distutils.core import setup
from Cython.Build import cythonize
setup(
ext_modules = cythonize("helloworld.pyx")
)
3). 编译环境
因为在进行编译时,需要用到编译环境,对于VC码农来说,这些安装比较简单,不再描述;
4). 编译
E:\zgmbt_tools\PyTools\PyShare\src\test>python setup.py build_ext --inplace
Compiling helloworld.pyx because it changed.
[1/1] Cythonizing helloworld.pyx
running build_ext
building 'src.test.helloworld' extension
d:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.23.28105\bin\HostX86\x86\cl.exe /c /nologo /Ox /W3 /GL /DNDEBUG /MT "-I
D:\Program Files (x86)\Python37-32\include" "-ID:\Program Files (x86)\Python37-32\include" "-Id:\Program Files (x86)\Microsoft Visual Studio\2019\Comm
unity\VC\Tools\MSVC\14.23.28105\ATLMFC\include" "-Id:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.23.28105\include" "-
IC:\Program Files (x86)\Windows Kits\NETFXSDK\4.8\include\um" "-ID:\Windows Kits\10\include\10.0.18362.0\ucrt" "-ID:\Windows Kits\10\include\10.0.1836
2.0\shared" "-ID:\Windows Kits\10\include\10.0.18362.0\um" "-ID:\Windows Kits\10\include\10.0.18362.0\winrt" "-ID:\Windows Kits\10\include\10.0.18362.
0\cppwinrt" /Tchelloworld.c /Fobuild\temp.win32-3.7\Release\helloworld.obj
helloworld.c
d:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.23.28105\bin\HostX86\x86\link.exe /nologo /INCREMENTAL:NO /LTCG /nodefa
ultlib:libucrt.lib ucrt.lib /DLL /MANIFEST:EMBED,ID=2 /MANIFESTUAC:NO "/LIBPATH:D:\Program Files (x86)\Python37-32\libs" "/LIBPATH:D:\Program Files (x
86)\Python37-32\PCbuild\win32" "/LIBPATH:d:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.23.28105\ATLMFC\lib\x86" "/LIB
PATH:d:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.23.28105\lib\x86" "/LIBPATH:C:\Program Files (x86)\Windows Kits\NE
TFXSDK\4.8\lib\um\x86" "/LIBPATH:D:\Windows Kits\10\lib\10.0.18362.0\ucrt\x86" "/LIBPATH:D:\Windows Kits\10\lib\10.0.18362.0\um\x86" /EXPORT:PyInit_he
lloworld build\temp.win32-3.7\Release\helloworld.obj /OUT:E:\zgmbt_tools\PyTools\PyShare\src\test\src\test\helloworld.cp37-win32.pyd /IMPLIB:build\tem
p.win32-3.7\Release\helloworld.cp37-win32.lib
Creating library build\temp.win32-3.7\Release\helloworld.cp37-win32.lib and object build\temp.win32-3.7\Release\helloworld.cp37-win32.exp
Generating code
Finished generating code
5).测试
这时候请注意,先找到helloworld.xxx.pyd文件(没有在当前目录,请找到后拷贝到当前目录)。
然后启动cmd,Python,import helloworld
就直接输出helloworld
例2,支持C++(为了简单起见,直接覆盖helloworld.pyx):
from libcpp.string cimport string
from libcpp.vector cimport vector
py_bytes_object = b'The knights who say ni'
py_unicode_object = u'Those who hear them seldom live to tell the tale.'
cdef string s = py_bytes_object
print(s) # b'The knights who say ni'
cdef string cpp_string = <string> py_unicode_object.encode('utf-8')
print(cpp_string) # b'Those who hear them seldom live to tell the tale.'
cdef vector[int] vect = range(1, 10, 2)
print(vect) # [1, 3, 5, 7, 9]
cdef vector[string] cpp_strings = b'It is a good shrubbery'.split()
print(cpp_strings[1]) # b'is'
测试结果;
E:\zgmbt_tools\PyTools\PyShare\src\test\src\test>python
Python 3.7.0 (v3.7.0:1bf9cc5093, Jun 27 2018, 04:06:47) [MSC v.1914 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import helloworld
b'The knights who say ni'
b'Those who hear them seldom live to tell the tale.'
[1, 3, 5, 7, 9]
b'is'