python查看安装包位置 查看sip是多少位

您现在的位置:&&>>&&>>&&>>&&>>&正文
Python&SIP使用总结
  Python中使用C/模块有许多工具,大名鼎鼎的有SWIG(英文意思为:豪饮)、SIP(英文意思为:啜饮,小口的喝),还有boost.python等。其中SIP是从SWIG发展而来,专为Python调用C/模块使用的(看SIP的命名就能看出来,很有意思的)。
  SIP的使用方法,在官网的SIP Reference Guide中有介绍,不过那是针对至少有点经验的人员,对初学者来说,想十分钟快速上手还是有点难度的。这里就介绍下本人在使用SIP总结的需要注意的地方。(本着方便大家方便自己的原则,欢迎转载,无需征得本人同意,但请注明出处,最好是原文连接,图个人气,谢谢!)
  前提,本机上要装了Python、GCC(下可以用MinGW,在我的Eclipse下配置CDT一文中有关于MinGW的安装介绍)、SIP(SIP的安装不难,可以对着SIP Reference Guide的Installation那一节做,安装SIP时就需要GCC了)。
  1、首先,编写个C文件,功能是将两个数字相加并输出,命名为add.c,这个将成为在Python中的模块名,如下:
  [cpp] view plaincopy /* File : add.c */ int add(int x, int y)
  { int g;g = x + y;return g;} 2、接着,手工编写SIP文件(类似C/C++的头文件,编写规范参考SIP Reference Guide,不难,有C/C++基础的人十几分钟就可以了,当然,你要懂鸟语),命名为add.sip,如下:
  [plain] view plaincopy /* Define the SIP wrapper to the add library. */ %Module(name=add, language="C")
  int add(int x, int y);3、编译C文件。按照官网的说法,是编写configure.py,但别急,先做一些必不可少的工作。在命令行将add.c编译成add.o文件:输入
  [sql] view plaincopy gcc -c add.c接着,将这一步生成的add.o文件生产库文件:
  [plain] view plaincopy ar -r libadd.a add.o将libadd.a文件复制到Python文件夹下的libs文件夹中(这两步在这里是为一个单独的C模块的,如果是大量的C模块,可以用一个makefile一并批量完成,这也是初学者容易模糊的地方)。
  4、手工编写configure.py文件,同样,这个configure文件的编写也不难,看下规范就会了(要会鸟语……)。这里,我们模仿官网的模版写一个自己的configure.py.
  [python] view plaincopy import os import sipconfig
  # The name of the SIP build file generated by SIP and used by the build # system. build_file = "add.sbf"
  # Get the SIP configuration information. config = sipconfig.Configuration()
  # Run SIP to generate the code. os.system(" ".join([config.sip_bin, "-c", ".", "-b", build_file, "add.sip"]))
  # Create the Makefile. makefile = sipconfig.SIPModuleMakefile(config, build_file)
  # Add the library we are wrapping.& The name doesn't include any platform # specific prefixes or extensions (e.g. the "lib" prefix on UNIX, or the # ".dll" extension on )。
  makefile.extra_libs = ["add"]
  # Generate the Makefile itself. makefile.generate()
  5、运行configure.py,会生成一个makefile文件(直接用IDLE打开configure.py,按F5运行;或者命令行用python configure.py运行都可以)。
  6、在命令行输入make(这里会生成一个关于函数的警告,不用管它,我们是用来的……其他的应该没什么问题,若有问题请检查前面的步骤),生成add.pyd文件。然后再输入make install(将add.pyd文件装入到Python的Lib文件夹下的sit-packages文件夹中)。
  7、打开Python 的命令行,进行测试:
  [python] view plaincopy
&&&import add&&&add.add(4, 7)
  (原谅我这么烂的模块名……)
  提示:
  (1)、这些文件可以放到Python的文件夹下新建的文件夹中(所有的操作在这个目录下的命令行窗口中使用)。注意,Python的父文件夹名不能有空格,否则会无法读取库文件。
  (2)、使用MinGW,需要把~\MinGW\bin添加的环境变量中(Linux下则不必),这样才能使用gcc、make和ar等工具。
  若还有什么注意的,我回继续补充。
【责编:eric】
?&[]?&[]?&[]?&[]?&[]?&[]?&[]?&[]?&[]?&[]
相关产品和培训
 友情推荐链接
 专题推荐
 ? ? ? ? ? ? ? ? ? ?
 今日更新
?&?&?&?&?&?&?&?&?&?&
 认证培训
 频道精选
 Windows频道导航
                      Python SIP使用总结(Win&Linux通用)
来源:csdn
【本文原先发表与我的博客。这是我做图像处理的时候摸索到的,现分享给大家。在用Python+OpenCV做影像分割、识别以及变换检测时,有时候会遇到处理速度瓶颈,于是想到用C++重写一些模块。在观察各个Python调用C++的方法中,在网上看了许多评价和介绍,最后选择了SIP作为工具。
本着分享的精神,现将使用的方法和在使用中遇到的问题总结出来,供大家参考。欢迎拍砖。本文也参加CSDN技术大分享活动,欢迎大家捧场。
如前所述,Python中使用C/C++模块有许多工具,大名鼎鼎的有SWIG(英文意思为:豪饮)、SIP(英文意思为:啜饮,小口的喝),还有boost.python等。其中SIP是从SWIG发展而来,专为Python调用C/C++模块使用的(看SIP的命名就能看出来,很有意思的)。
或许有人问为什么不用ctypes模块,原因是使用SIP无需编写特定格式的C/C++文件。需要调用的C/C++源代码文件,只是最简单的函数即可。若说SIP有什么需要学习的,那就是sip文件(下文中会介绍到)的编写。不过sip文件是基于C/C++的头文件的,学起来也比较简单。
SIP的使用方法,在官网的SIP Reference Guide中有介绍,不过那是针对至少有点经验的人员,对初学者来说,想十分钟快速上手还是有点难度的。这里就介绍下本人在使用SIP总结的需要注意的地方。
本机上要装了Python、GCC(Windows下可以用MinGW)。并把python.exe和MinGW下的bin文件路径添加到环境变量中。
将Riverbank上的sip的zip格式的代码包下载,将其解压到C:\Python27中,那么现在sip的文件夹路径为C:\Python27\sip-4.13.2\sip-4.13.2(你也可以将其解压到任何一个文件夹中,但这里为了叙述方便,就解压到我电脑上的Python的文件夹中)。
打开Windows的cmd命令行,输如cd C:\Python27\sip-4.13.2\sip-4.13.2跳转到sip文件夹下。
输入python configure.py --platform win32-g++。进行配置
再完成上一步后,输入mingw32-make,接着输入mingw32-make install。若无意外,SIP就安装完成了。
若是你的电脑上之前安装了Eclipse等其他软件,而将mingw32-make改成了make,那么就要将上面的mingw32-make命令改为make来使用。
有个朋友说他按上面的方法使用make总是报错,仔细一问,才发现他的电脑上安装了Embacardero的RAD开发工具,也就是熟知的Delphi和C++ Builder的开发工具。在安装了这个工具后,在命令行中输入make,使用的总是Embacardero的C++ Builder的make。根据亲测,即使在配置的时候,使用python configure.py --platform win32-borland命令进行配置,对Emcarbadero的make也是无效的。这个配置命令产生的make文件,只是用于老版本的Borland C++ Builder的make命令。
若要使用微软的VC的nmake,就要在配置时使用命令python configure.py --platform win32-msvc,其中win32-msvc使用的是VC6的nmake;win32-msvc.net使用的是VC2003的nmake;win32-msvc2005使用的是VC2005的nmake;win32-msvc2008使用的是VC2008的nmake。根据--show-platforms命令显示的结果,SIP的配置暂不支持VC2010和VS11下的nmake。若想知道SIP支持的所有平台和编译器,可以在命令行中使用python configure.py --show -platforms命令来查看。
Linux下的SIP安装与之类似,甚至更简单,Linux的发行版如Ubuntu,都自动安装了python和gcc,无需配置环境变量。
所以只要在官网上下载SIP的Linux下的tar.gz格式的代码包,解压到某个目录中。然后在终端中进入该目录,依次输入python configure.py --platform linux-g++;make install即可。
,编写个C文件,功能是将两个数字相加并输出,命名为add.c,这个将成为在Python中的模块名,如下:
,手工编写SIP文件,在使用SIP的过程中,一个C/C++的源码文件,必须对应一个同名的sip文件,命名为add.sip,如下:
如果是源程序是用C++写的,那么这里的(name=add, language="C")就可以省去。
这里的C源码文件没有头文件,所以对应的sip文件很简单。如果C/C++的源码是实现部分,在实现部分还包括接口部分,即头文件。那么在相应的sip文件中需要用
来包含相应的头文件。sip文件与正式的C/C++头文件相似,但与之不同的是:sip文件不包含相应的头文件的私有成员变量(private或protected)。更详细的sip文件编写规范,请参考riverbank官方网站上的说明文档SIP Reference Guide。
。按照官网的说法,是编写configure.py,但别急,先做一些必不可少的工作。在命令行将add.c编译成add.o文件:输入
gcc -c add.c
接着,将这一步生成的add.o文件生产库文件:
ar -r libadd.a add.o
这两步在这里是为一个单独的C模块测试的,如果是大量的C模块,可以用一个makefile一并批量完成,这也是初学者容易模糊的地方。 也可以将源代码直接编译成dll,命令为gcc --shared add.c -o add.dll,
,同样,这个configure文件的编写也不难,看下规范就会了(要会鸟语。。。)。这里,我们模仿官网的模版写一个自己的configure.py。
,会生成一个makefile文件(直接用IDLE打开configure.py,按F5运行;或者命令行用python configure.py运行都可以)。
这里有个诡异的地方,有几个朋友在这一步会报错,说找不到add.sbf文件,而add.sbf文件应该是configure.py运行时调用相关函数自动产生的。若出现这个问题,请重新编译SIP。如果是Windows下,最好是在另一台机器上拷贝一个完整的包含能正常的SIP的Python文件夹,到有问题的机器上,将问题Python文件夹覆盖掉。
(这里会生成一个关于函数的警告,不用管它,我们是用来测试的。。。其他的应该没什么问题,若有问题请检查前面的步骤),生成add.pyd文件。然后再输入make install(将add.pyd文件装入到Python的Lib文件夹下的sit-packages文件夹中)。
(原谅我这么烂的模块名。。。)
(1)、这些文件可以放到Python的文件夹下新建的文件夹中(所有的操作在这个目录下的命令行窗口中使用)。注意,Python的父文件夹名不能有空格,否则会无法读取库文件。】
maco_wang:
步骤很清晰,谢谢分享!
fox000002:
该回复于 09:09:29被版主删除
今天不上班,刚好学下这个东西
ZuxiangHuang:
.不错,学学
lichun704:
请问一个类的C++代码如何用SIP将其导入Python?初学者,谢谢。
sunny2038:
具体的实现办法和上面介绍的相似,所不同的就是根据相应的C++头文件来写对应的sip文件。详细过程如下。
首先,写好C++类的实现和接口部分(以Geometry类中的方法,输入半径,显示周长或面积为例):
//Geometry.h
class Geometry
Geometry(double r); //构造器
double calPerimeter(double radius);
//参数半径,返回圆周长
double calArea(double radius);
//参数半径,返回原面积
接着,是C++的实现部分:
//Geometry.cpp
#include &iostream&
#include "Geometry.h"
#define PI 3.141592
Geometry::Geometry(double r) //构造器
double Geometry::calPerimeter(double radius)
//参数半径,返回圆周长
perimeter = radius * 2* PI;
double Geometry::calArea(double radius)
//参数半径,返回原面积
area = radius * radius * PI;
第三步:如同正文中介绍的,在命令行中用g++ -c Geometry.cpp和ar -r libGeometry.a Geometry.o来编译出库文件,并把库文件添加到Python文件夹下的libs文件夹中。
第四步:编写C++文件对应的sip文件,再次提示,sip文件是基于C++的头文件的。本例中的sip文件内容为:
//Geometry.sip
%Module Geometry
class Geometry {
%TypeHeaderCode
#include &Geometry.h&
Geometry(double r);
double calPerimeter(double radius);
double calArea(double radius);
注:sip文件编写的格式我将在我的博客中陆续译出。
第五步:在命令行中使用Python configure.py命令,注意,configure.py文件是自己写的,configure的模板在正文中有,只需将正文中的模板中的add改为对应的Geometry,也就是类名就可以了。
第六步:在命令行中依次使用mingw32-make和ming32-make install将编译出的Geometry.pyd复制到Python文件夹下的Libs/site-packages文件夹中。
若不出意外,上述步骤可以执行成功。注:这里所有的命令都是在Geometry类的文件所在目录下完成的。
sunny2038:
注:经亲测,已测试成功。因为是测试的,所以类的构造器和方法的重载并不完善。实例如下:
&&& import Geometry
&&& a = Geometry.Geometry(1)
&&& a.calPerimeter(1)
sunny2038:
如果您喜欢本文或者感觉本文对您有帮助,请到投票支持一下,谢谢,仅占用您1分钟的时间。本作品在列表的倒数第5个位置。
cehuikaoyan:
学习学习 楼主以后多多帮助
yousteely:
分享是一种美德
嗯, 同swig极像,有空再看看...
lichun704:
多谢解答!
该回复于 22:35:31被版主删除
写的不错,虽然我不懂
但是感觉要是有截图就完美了~~
正好学习下,标题应该改为“使用”SIP在Python中调用C/C++,你这个标题会让人看不懂的
mymandy610:
写的很好,正在看这块的内容。目前看到的最好的入门“教程”了。
有办法在python项目里调试生成的pyd文件吗,环境是eclipse
有办法在python项目里调试生成的pyd文件吗,环境是eclipse
或者能不能在vc里启动python,项目属性里设置:调试——命令:python,命令参数:**.py。然后启动调试。这样可不可以,请高手指教。
xuyanjing1216:
初学者,谢谢大师分享
myseemydog:
牛逼。。真是好东西
hesiyuan4:
sip 是否支持使用
dll 动态链接库的调用?
在现有模块基础上,不想再次把所有文件都编译一遍, 提供dll, 以及相关头文件, 是否可以使用SIP 模块对相关函数,以及类调用?
免责声明:本站部分内容、图片、文字、视频等来自于互联网,仅供大家学习与交流。相关内容如涉嫌侵犯您的知识产权或其他合法权益,请向本站发送有效通知,我们会及时处理。反馈邮箱&&&&。
学生服务号
在线咨询,奖学金返现,名师点评,等你来互动Build Sip with Python 2.7.3 64bit with MingW - 推酷
Build Sip with Python 2.7.3 64bit with MingW
I Try to build SIP against my 3DSMax Python 2.7.3 64bit version, because it’s not shipped with.
I configure it with:
python configure.py --platform win32-g++
and try to compile it with:
C:\MinGW\bin\mingw32-make.exe
C:\MinGW\bin\mingw32-make.exe install
it gives me following error:
collect2.exe: error: ld returned 1 exit status
Makefile:36: recipe for target 'sip.pyd' failed
mingw32-make[1]: *** [sip.pyd] Error 1
mingw32-make[1]: Leaving directory 'C:/Users/myuser/Desktop/sip-4.1
6.9/siplib'
Makefile:3: recipe for target 'all' failed
mingw32-make: *** [all] Error 2
it works like a acharm with python 2.7.8. 32 bit. I think it’s because of the platform flag, but i can’t fint a flag for 64bit windows. I don’t really get into my head how to compile 64 bit with ming32.
a small hint to get rid of the knot in my head would be awesome. thanks in advance
已发表评论数()
请填写推刊名
描述不能大于100个字符!
权限设置: 公开
仅自己可见
正文不准确
标题不准确
排版有问题
主题不准确
没有分页内容
图片无法显示
视频无法显示
与原文不一致Python API for Applications & SIP 4.18.1 Reference Guide
Python API for Applications
The main purpose of the
module is to provide functionality common to
all SIP generated bindings.
It is loaded automatically and most of the time
you will completely ignore it.
However, it does expose some functionality that
can be used by applications.
class sip.array
New in version 4.15.
This is the type object for the type SIP uses to represent an array of a
limited number of C/C++ types.
Typically the memory is not owned by Python
so that it is not freed when the object is garbage collected.
object can be created from a
by calling .
This allows the underlying memory
(interpreted as a sequence of unsigned bytes) to be processed much more
sip.cast(obj, type) & object
This does the Python equivalent of casting a C++ instance to one of its
sub or super-class types.
Parameters:
obj – the Python object.
type – the type.
Returns:a new Python object is that wraps the same C++ instance as obj, but
has the type type.
sip.delete(obj)
For C++ instances this calls the C++ destructor.
For C structures it
returns the structure’s memory to the heap.
Parameters:obj – the Python object.
sip.dump(obj)
This displays various bits of useful information about the internal state
of the Python object that wraps a C++ instance or C structure.
Parameters:obj – the Python object.
sip.enableautoconversion(type, enable) & bool
New in version 4.14.7.
Instances of some classes may be automatically converted to other Python
objects even though the class has been wrapped.
This allows that behaviour
to be suppressed so that an instances of the wrapped class is returned
Parameters:
type – the Python type object.
enable – is True if auto-conversion should be enabled for the type.
the default behaviour.
Returns:True or False depending on whether or not auto-conversion was
previously enabled for the type.
This allows the previous state to be
restored later on.
sip.getapi(name) & version
New in version 4.9.
This returns the version number that has been set for an API.
The version
number is either set explicitly by a call to
implicitly by importing the module that defines it.
Parameters:name – the name of the API.
Returns:The version number that has been set for the API.
An exception will
be raised if the API is unknown.
sip.isdeleted(obj) & bool
This checks if the C++ instance or C structure has been deleted and
returned to the heap.
Parameters:obj – the Python object.
Returns:True if the C/C++ instance has been deleted.
sip.ispycreated(obj) & bool
New in version 4.12.1.
This checks if the C++ instance or C structure was created by Python.
it was then it is possible to call a C++ instance’s protected methods.
Parameters:obj – the Python object.
Returns:True if the C/C++ instance was created by Python.
sip.ispyowned(obj) & bool
This checks if the C++ instance or C structure is owned by Python.
Parameters:obj – the Python object.
Returns:True if the C/C++ instance is owned by Python.
sip.setapi(name, version)
New in version 4.9.
This sets the version number of an API.
An exception is raised if a
different version number has already been set, either explicitly by a
previous call, or implicitly by importing the module that defines it.
Parameters:
name – the name of the API.
version – The version number to set for the API.
Version numbers must be
greater than or equal to 1.
sip.setdeleted(obj)
This marks the C++ instance or C structure as having been deleted and
returned to the heap so that future references to it raise an exception
rather than cause a program crash.
Normally SIP handles such things
automatically, but there may be circumstances where this isn’t possible.
Parameters:obj – the Python object.
sip.setdestroyonexit(destroy)
New in version 4.14.2.
When the Python interpreter exits it garbage collects those objects that it
This means that any corresponding C++ instances and C structures
owned by Python are destroyed.
Unfortunately this happens in an
unpredictable order and so can cause memory faults within the wrapped
Calling this function with a value of False disables the
automatic destruction of C++ instances and C structures.
Parameters:destroy – True if all C++ instances and C structures owned by Python should
be destroyed when the interpreter exits.
This is the default.
sip.settracemask(mask)
If the bindings have been created with SIP’s
line option then the generated code will include debugging statements that
trace the execution of the code.
(It is particularly useful when trying to
understand the operation of a C++ library’s virtual function calls.)
Parameters:mask – the mask that determines which debugging statements are enabled.
Debugging statements are generated at the following points:
in a C++ virtual function (mask is 0x0001)
in a C++ constructor (mask is 0x0002)
in a C++ destructor (mask is 0x0004)
in a Python type’s __init__ method (mask is 0x0008)
in a Python type’s __del__ method (mask is 0x0010)
in a Python type’s ordinary method (mask is 0x0020).
By default the trace mask is zero and all debugging statements are
class sip.simplewrapper
This is an alternative type object than can be used as the base type of an
instance wrapped by SIP.
Objects using this are smaller than those that
use the default
type but do not support the concept of
object ownership.
sip.SIP_VERSION
New in version 4.2.
This is a Python integer object that represents the SIP version number as
a 3 part hexadecimal number (e.g. v4.0.0 is represented as 0x040000).
sip.SIP_VERSION_STR
New in version 4.3.
This is a Python string object that defines the SIP version number as
represented as a string.
For development versions it will contain either
.dev or -snapshot-.
sip.transferback(obj)
This function is a wrapper around .
sip.transferto(obj, owner)
This function is a wrapper around .
sip.unwrapinstance(obj) & integer
This returns the address, as an integer, of a wrapped C/C++ structure or
class instance.
Parameters:obj – the Python object.
Returns:an integer that is the address of the C/C++ instance.
class sip.voidptr
This is the type object for the type SIP uses to represent a C/C++
It may have a size associated with the address in which case
the Python buffer interface is supported.
The type has the following
__init__(address[, size=-1[, writeable=True]])
Parameters:
address – the address, either another , None, a
Python Capsule, a Python CObject, an object that implements the
buffer protocol or an integer.
size – the optional associated size of the block of memory and is negative
if the size is not known.
writeable – set if the memory is writeable.
If it is not specified, and
address is a
instance then its value will be
__int__() & integer
This returns the address as an integer.
Returns:the integer address.
__getitem__(idx) & item
New in version 4.12.
This returns the item at a given index.
An exception will be raised if
the address does not have an associated size.
In this way it behaves
like a Python memoryview object.
Parameters:idx – is the index which may either be an integer, an object that
implements __index__() or a slice object.
Returns:the item.
If the index is an integer then the item will be a
Python v2 string object or a Python v3 bytes object containing the
single byte at that index.
If the index is a slice object then the
item will be a new
object defining the subset of
the memory corresponding to the slice.
__hex__() & string
This returns the address as a hexadecimal string.
Returns:the hexadecimal string address.
__len__() & integer
New in version 4.12.
This returns the size associated with the address.
Returns:the associated size.
An exception will be raised if there is none.
__setitem__(idx, item)
New in version 4.12.
This updates the memory at a given index.
An exception will be raised
if the address does not have an associated size or is not writable.
this way it behaves like a Python memoryview object.
Parameters:
idx – is the index which may either be an integer, an object that
implements __index__() or a slice object.
item – is the data that will update the memory defined by the index.
must implement the buffer interface and be the same size as the
data that is being updated.
asarray([size=-1]) & :class:`sip.array`
New in version 4.16.5.
This returned the block of memory as a
memory is not copied.
Parameters:size – the size of the array.
If it is negative then the size associated
with the address is used.
If there is no associated size then an
exception is raised.
Returns:the
ascapsule() & capsule
New in version 4.10.
This returns the address as an unnamed Python Capsule.
This requires
Python v3.1 or later or Python v2.7 or later.
Returns:the Capsule.
ascobject() & cObject
This returns the address as a Python CObject.
This is deprecated with
Python v3.1 and is not supported with Python v3.2 and later.
Returns:the CObject.
asstring([size=-1]) & string/bytes
This returns a copy of the block of memory as a Python v2 string object
or a Python v3 bytes object.
Parameters:size – the number of bytes to copy.
If it is negative then the size
associated with the address is used.
If there is no associated
size then an exception is raised.
Returns:the string or bytes object.
getsize() & integer
This returns the size associated with the address.
Returns:the associated size which will be negative if there is none.
setsize(size)
This sets the size associated with the address.
Parameters:size – the size to associate.
If it is negative then no size is
associated.
getwriteable() & bool
This returns the writeable state of the memory.
Returns:True if the memory is writeable.
setwriteable(writeable)
This sets the writeable state of the memory.
Parameters:writeable – the writeable state to set.
sip.wrapinstance(addr, type) & object
This wraps a C structure or C++ class instance in a Python object.
instance has already been wrapped then a new reference to the existing
object is returned.
Parameters:
addr – the address of the instance as a number.
type – the Python type of the instance.
Returns:the Python object that wraps the instance.
class sip.wrapper
This is the type object of the default base type of all instances wrapped
class annotation can be used to specify a
different base type for a class.
class sip.wrappertype
This is the type object of the metatype of the

我要回帖

更多关于 python 查看模块位置 的文章

 

随机推荐