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'

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Jupyter是一个交互式编程环境,提供了一个网页界面来编辑和运行代码。Cython是一种编程语言,它可以通过类似Python的语法来编写C扩展,并且可以被Python调用。使用Cython编写的代码可以通过Jupyter来编辑、运行和测试。 对于mingw,它是一个可以在Windows系统上使用的GNU工具集合,包括编译器和各种实用工具。在使用Cython编写代码时,您可以选择使用mingw来编译和构建您的Cython扩展。mingw提供了一些优化和性能提升的功能,这对于需要提高代码执行速度的任务非常有用。 因此,如果您想在Jupyter中使用Cython编写代码,并使用mingw来编译和构建您的Cython扩展,您可以按照以下步骤进行操作: 1. 确保您已经安装了Jupyter Notebook和Cython。您可以使用pip安装Cython:`pip install cython`。 2. 确保您已经安装了mingw。您可以从官方网站下载mingw的安装程序,并按照提示进行安装。 3. 打开Jupyter Notebook,并创建一个新的代码单元格。 4. 在代码单元格中,使用Cython语法编写您的代码。 5. 使用Jupyter的内置魔术命令`%%cython`来编译和运行Cython代码。在魔术命令后面,您可以通过添加`--compiler=mingw32`选项来指定使用mingw编译器。例如:`%%cython --compiler=mingw32`。 6. 运行代码单元格,Jupyter将使用mingw编译并运行您的Cython代码。 这样,您就可以在Jupyter中使用Cython编写代码,并使用mingw编译和构建您的Cython扩展了。请注意,确保您已正确安装和配置了所需的软件和工具,以便顺利使用Cython和mingw。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值