Cython使用問題集合

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'

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章