python开发环境配置config_扩展Python模块系列(一)----开发环境配置

本系列将介绍如何用C/C++扩展Python模块,使用C语言编写Python模块,添加到Python中作为一个built-in模块。Python与C之间的交互目前有几种方案:

1. 原生的Python C/C++ API, 官网有非常详细的文档说明

2. boost python,一个C++的编程框架,对官方API进行了封装,可以方便的用C++扩展Python模块,省去了很多诸如引用计数的烦恼。 http://www.boost.org/doc/libs/1_64_0/libs/python/doc/html/index.html

3. Cython. Cython其实是一个可以优化Python程序的编译器,将Python代码翻译成C代码,然后使用C/C++编译器进行编译得到pyd(windows)或so(linux),在Python解释器中可以直接import。这种方法里面的坑有点多,在本系列博客中会介绍到Cython。   http://cython.org/

4. 谷歌最近开源的CLIF项目,同时支持Python2和Python3,目前来说还不太完善。  https://github.com/google/clif

其他的框架。。。。。。

本篇将介绍如何用官方提供的Python C/C++ API来扩展Python模块。工欲善其事必先利其器,先讲一下如何搭建开发环境(默认已经安装了Python)。以下为【DEBUG】版的配置:

1. Python版本: Python2.7 32位;   操作系统: Windows7;  IDE: Visual Studio 2015。

2. 配置Library Directories、Additional Include Directories、Link Input

3. 写一段hello world程序 ,先不纠结语法细节

[test.c]

#include

static PyMethodDef test_methods[] = { NULL, NULL, 0, NULL };

PyMODINIT_FUNC test_init(void)

{

Py_InitModule3("test", test_methods, "Common test Written in C.");

}

[Souce.cpp]

#include PyMODINIT_FUNC test_init();intmain()

{

Py_Initialize();

test_init();return 0;

}

然后编译,很大可能会出现Link时找不到解析符号以及无法找到python27_d.lib,这主要是pyconfig.h里代码的一些宏导致的。

1)找到python2.7/include/pyconfig.h,并找到

#ifdef _DEBUG#define Py_DEBUG

#endif

修改为

#ifdef _DEBUG//#define Py_DEBUG

#endif

2)Configuration Properties->C/C++->Preprocessor->Preprocessor Definitions添加MS_NO_COREDLL或Py_NO_ENABLE_SHARED

原因解释:

1)

在pyconfig.h中:

#ifdef _DEBUG#define Py_DEBUG

#endif

在object.h中有以下定义:

/*Py_DEBUG implies Py_TRACE_REFS.*/

#if defined(Py_DEBUG) && !defined(Py_TRACE_REFS)

#define Py_TRACE_REFS

#endif

在modesupport.h中

#ifdef Py_TRACE_REFS/*When we are tracing reference counts, rename Py_InitModule4 so

modules compiled with incompatible settings will generate a

link-time error.*/

#if SIZEOF_SIZE_T != SIZEOF_INT

#undef Py_InitModule4

#define Py_InitModule4 Py_InitModule4TraceRefs_64

#else

#define Py_InitModule4 Py_InitModule4TraceRefs

#endif

#endif

所以当定义了Py_DEBUG,就会导致Py_InitModule4重命名为Py_initModule4TraceRefs,而lib中没有此符号,所以导致了链接错误。故而注释掉#define Py_DEBUG这一行即可。

2)找不到python27_d.lib问题

同样在pyconfig.h中:

/*For Windows the Python core is in a DLL by default. Test

Py_NO_ENABLE_SHARED to find out. Also support MS_NO_COREDLL for b/w compat*/

#if !defined(MS_NO_COREDLL) && !defined(Py_NO_ENABLE_SHARED)# define Py_ENABLE_SHARED1 /*standard symbol for shared library*/# define MS_COREDLL/*deprecated old symbol*/

#endif /* !MS_NO_COREDLL && ... */

/*All windows compilers that use this header support __declspec*/

#define HAVE_DECLSPEC_DLL

/*For an MSVC DLL, we can nominate the .lib files used by extensions*/#ifdef MS_COREDLL

# ifndef Py_BUILD_CORE/*not building the core - must be an ext*/#ifdefined(_MSC_VER)/*So MSVC users need not specify the .lib file in

their Makefile (other compilers are generally

taken care of by distutils.)*/# ifdef _DEBUG

# pragma comment(lib,"python27_d.lib")

#else# pragma comment(lib,"python27.lib")

# endif/*_DEBUG*/# endif/*_MSC_VER*/# endif/*Py_BUILD_CORE*/

#endif /* MS_COREDLL */

可以看到如果没有定义MS_NO_COREDLL和Py_NO_ENABLE_SHARED时,会定义MS_COREDLL,然后在DEBUG模式下会加载python27_d.lib。所以在Preprocessor Definitions中定义MS_NO_COREDLL即可解决。

4. 编译&链接错误解决之后,可以正常运行。

上面的例子中,test.c定义了一个模块test,在Source.cpp中初始化了该模块。下一节将用一个例子来详细介绍使用Python C/C++ API扩展Python模块。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
ConfigArgParse是Python中的一个命令行参数解析模块,它支持从多个来源(命令行参数、配置文件、环境变量等)读取参数,并且提供了一些额外的功能,如默认值、类型转换、互斥参数等。 它的使用方式类似于Python自带的argparse模块,但是提供了更加灵活的参数读取方式。同时,ConfigArgParse也支持将参数值保存到配置文件中,方便下次使用时直接读取。 以下是一个使用ConfigArgParse模块的例子: ```python import configargparse parser = configargparse.ArgumentParser() parser.add_argument('--input', type=str, help='input file path') parser.add_argument('--output', type=str, help='output file path') parser.add_argument('--num_iters', type=int, default=10, help='number of iterations') parser.add_argument('--batch_size', type=int, default=32, help='batch size') args = parser.parse_args() print(args.input) print(args.output) print(args.num_iters) print(args.batch_size) ``` 在上述例子中,我们使用了configargparse.ArgumentParser()创建了一个解析器对象,并通过add_argument()方法添加了四个参数。其中,--input和--output是必选参数,--num_iters和--batch_size是可选参数,如果不指定则会使用默认值。 通过parser.parse_args()方法解析命令行参数,可以从命令行读取参数值来覆盖默认值。例如,运行以下命令: ```bash python example.py --input data.txt --num_iters 20 ``` 则会输出: ``` data.txt None 20 32 ``` 其中,--input参数的值被设为了"data.txt",--num_iters参数的值被设为了20,而--output参数没有指定,因此其值为None。 除了从命令行读取参数外,ConfigArgParse还支持从配置文件、环境变量、文件等多个来源读取参数。例如,可以在当前目录下创建一个config.ini文件,写入以下内容: ```ini [input] path = data.txt [output] path = result.txt [num_iters] value = 30 [batch_size] value = 64 ``` 然后,在命令行中使用以下命令运行脚本: ```bash python example.py --config-file config.ini ``` 则会从配置文件中读取参数值,并覆盖默认值。例如,在上述例子中,--input参数的默认值被覆盖为了"data.txt",而--num_iters参数的默认值被覆盖为了30。 ConfigArgParse还支持其他一些高级功能,如互斥参数、子命令等,可以根据实际需求进行使用。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值