简体   繁体   English

如何为绝对的初学者制作适用于Windows的Python扩展

[英]How to make Python Extensions for Windows for absolute beginners

I've been looking around the internet trying to find a good step by step guide to extend Python in Windows, and I haven't been able to find something for my skill level. 我一直在寻找互联网,试图找到一个很好的分步指南,在Windows中扩展Python,我无法找到适合我技能水平的东西。

let's say you have some c code that looks like this: 假设你有一些看起来像这样的c代码:

#include <stdio.h>
#include <math.h>

double valuex(float value, double rate,  double timex)
{
    float value;
    double rate, timex;
    return value / (double) pow ((1 + rate), (timex));
}

and you want to turn that into a Python 3 module for use on a windows (64bit if that makes a difference) system. 并且你想把它变成一个Python 3模块,用于Windows(64位,如果这有所不同)系统。 How would you go about doing that? 你会怎么做呢? I've looked up SWIG and Pyrex and in both circumstances they seem geared towards the unix user. 我查了一下SWIG和Pyrex,在这两种情况下他们似乎都面向unix用户。 With Pyrex I am not sure if it works with Python 3. 使用Pyrex我不确定它是否适用于Python 3。

I'm just trying to learn the basics of programing, using some practical examples. 我只是尝试使用一些实际的例子来学习编程的基础知识。

Lastly, if there is a good book that someone can recommend for learning to extend, I would greatly appreciate it. 最后,如果有一本好书可以推荐学习扩展,我会非常感激。

Thank you. 谢谢。

Cython (Pyrex with a few kinks worked out and decisions made for practicality) can use one code base to make Python 2 and Python 3 modules. Cython(具有一些扭结的Pyrex和为实用性做出的决定)可以使用一个代码库来制作Python 2和Python 3模块。 It's a really great choice for making libraries for 2 and 3. The user guide explains how to use it, but it doesn't demystify Windows programming or C or Python or programming in general, thought it can simplify some things for you. 这是为2和3制作库的一个非常好的选择。 用户指南解释了如何使用它,但它并没有揭开Windows编程或C或Python或一般编程的神秘面纱,认为它可以简化一些事情。

SWIG can be hard to work with when you run into a problem and will not be especially conducive to creating a very native-feeling, idiomatic binding of the C you are relying on. 遇到问题时,SWIG可能难以使用,并且不会特别有助于创建您所依赖的C的本地感觉,惯用的绑定。 For that, you would need to re-wrap the wrapper in Python, at which point it might have been nicer just to use Cython. 为此,您需要在Python中重新包装包装器,此时使用Cython可能更好。 It can be nice for bindings that you cannot dedicate enough work to make truly nice, and is convenient in that you can expose your API to many languages at once in it. 它可以很好地用于绑定,你不能投入足够的工作来使真正的好,并且方便的是你可以在其中一次将API暴露给多种语言。

Depending on what you're trying to do, building your "extension" as a simple DLL and accessing it with ctypes could be, by far, the simplest approach. 根据您要做的事情,将“扩展”构建为一个简单的DLL并使用ctypes访问它可能是迄今为止最简单的方法。

I used your code, slightly adjusted and saved as mydll.c : 我使用了你的代码,略微调整并保存为mydll.c

#include <stdio.h>
#include <math.h>

#define DLL_EXPORT __declspec(dllexport)
DLL_EXPORT double valuex(float value, double rate,  double timex)
    {
    float value;
    double rate, timex;
    return value / (double) pow ((1 + rate), (timex));
    }

I downloaded the Tiny C Compiler and invoked with this command. 我下载了Tiny C Compiler并使用此命令调用。

tcc -shared mydll.c

(I believe adding -rdynamic would avoid the need to sprinkle DLL_EXPORT all over your function defs.) (我相信加入-rdynamic将避免需要洒DLL_EXPORT在你的功能DEFS)。

This generated mydll.dll . 这生成了mydll.dll I then ran Python: 我然后运行Python:

Python 3.1.2 (r312:79149, Mar 21 2010, 00:41:52) ... on win32
>>> from ctypes import *
>>> mydll = cdll.mydll
>>> valuex = mydll.valuex
>>> valuex.argtypes = [c_float, c_double, c_double]
>>> valuex.restype = c_double
>>> valuex(1.2, 2.3, 3.4)
2.0470634033800796e-21

一个开始是Windows上的构建C和C ++扩展的文档。

Well, the easiest way to create Python plugins is to use C++ and Boost.Python. 嗯,创建Python插件的最简单方法是使用C ++和Boost.Python。 In your example, the extension module would look as simple as this: 在您的示例中,扩展模块看起来就像这样简单:

#include <boost/python.hpp>
using namespace boost::python;

// ... your valuex function goes here ...

BOOST_PYTHON_MODULE(yourModuleName)
{
    def("valuex", valuex, "an optional documentation string");
}

Boost.Python is available on the popular operating systems and should work with Python 3, too (not tested it, though, support was added in 2009). Boost.Python可以在流行的操作系统上使用,也可以与Python 3一起使用(未经过测试,但是在2009年增加了支持)。

Regarding SWIG: It is not for Unix only. 关于SWIG:它不仅适用于Unix。 You can download precompiled Windows binaries or compile it yourself with MinGW/MSYS. 您可以下载预编译的Windows二进制文件或使用MinGW / MSYS自行编译。

You could as well try out Cython which is said to be Python 3 compatible . 你也可以尝试一下据说与Python 3兼容的 Cython。

At PyCon 2009, I gave a talk on how to write Python C extensions: A Whirlwind Excursion through Python C Extensions . 在PyCon 2009上,我讲了如何编写Python C扩展: 通过Python C Extensions进行旋风游览 There's nothing specific to Windows in it, but it covers the basic structure of an extension. Windows中没有特定的内容,但它涵盖了扩展的基本结构。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM