Cython的安装

Windows下安装

 先从http://cython.org下载软件包:Cython-0.19.2.zip

解压到一个目录,如D:\Cython-0.19.2

从开始菜单里打开Visual Studio Command Prompt,例如:MicroSoft Visual Studio  2010-> Visual Studio Tools -> Visual Studio Command Prompt(2010)

敲入如下命令:

D:

cd Cython-0.19.2

python setup.py install

命令执行完后就会把cython安装到python的安装目录下

2.2 Ubuntu下安装

apt-get install cython

3 第一个例子:

3.1 创建helloworld目录

创建helloworld.pyx,内容如下:

cdef extern from"stdio.h":

    extern int printf(const char *format, ...)

def SayHello():

printf("hello,world\n")

   

代码非常简单,就是调用了C函数printf打印hello,world

4 如何编译

4.1 最方便的当然是利用python的Distutils了,看下如何来实现

先在helloworld目录下创建Setup.py,内容如下:

from distutils.core import setup

from distutils.extension import Extension

from Cython.Build import cythonize

 

setup(

  name = 'helloworld',

  ext_modules=cythonize([

    Extension("helloworld", ["helloworld.pyx"]),

    ]),

)

 

 

编译:

python Setup.py build

安装:

python Setup.py install

安装后,会将在build/lib.???目录下生成的helloworld.pyd拷贝到Lib/site-packages

注:

  有时我们只是希望测试一下,并不希望安装,这时可以把build/lib.???目录下的helloworld.pyd拷贝到当前目录

  或者在importhelloworld前执行脚本:import sys;sys.path.append(pathof helloworld.pyd)

 

测试:

>>>import helloworld

>>>helloworld.SayHello()

hello,world

4.2 其次就是可以自己写Makefile进行编译

写Makefile的好处就是可以知道编译的实质:

下面是用于Windows下编译的Makefile,Makefile内容如下:

ALL :helloworld.pyd

helloworld.c : helloworld.pyx

     cython -o helloworld.c helloworld.pyx

helloworld.obj :helloworld.c

     cl -c -Id:\python27\include helloworld.c

helloworld.pyd :helloworld.obj

     link /DLL /LIBPATH:d:\python27\libshelloworld.obj /OUT:helloworld.pyd

 

执行命令:

set PATH=D:\Python27\Scripts;%PATH%

nmake

 

进行编译,会在根目录下生成helloworld.pyd

linux下的Makefile和Windows下的类似,只是编译器不同而己,另外,生成的文件名为:helloworld.so,而不是helloworld.pyd

 

测试:

>>>import helloworld

>>>helloworld.SayHello()

hello,world

要在Windows安装Cython,有几个方法可以选择。其中一种方法是通过官方Cython提供的压缩包进行安装。你可以从http://www.cython.org/#download下载压缩包,并解压到一个目录中。然后,在解压目录中打开命令提示符,并运行命令python setup.py install安装Cython。 另一种方法是解决Python环境的问题。你可以在Python安装目录下找到C:\Python27\Lib\distutils文件夹中的cygwinccompiler.py文件。在这个文件中,删除所有包含'-mno-cygwin'字符串的行,然后保存文件。这样做后,你就可以成功安装Cython[2]。 无论你选择哪种方法,都需要确保你的系统环境变量Path中包含了Python安装目录。这样,你就可以在命令提示符中运行python setup.py install命令来安装Cython。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [【CythonCythonWindows环境下的部署安装](https://blog.csdn.net/ztf312/article/details/77294537)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* *3* [Windows Cython 安装](https://blog.csdn.net/productshop/article/details/50477500)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值