简体   繁体   English

在Windows上构建64位C Python扩展

[英]Building 64-bit C Python extensions on Windows

I am asking this question because I need to build a specific module (aspell_python, http://wm.ite.pl/proj/aspell-python/ ) to work with my 64-bit Python 2.6 which runs on a Windows 7 (64-bit of course) machine. 我问这个问题是因为我需要构建一个特定的模块(aspell_python, http ://wm.ite.pl/proj/aspell-python/)来处理在Windows 7上运行的64位Python 2.6(64当然是机器)。 I also always wanted to know how to speed up certain functions with C code so I'd like to make my own external C modules for Python in the future. 我也一直想知道如何使用C代码加速某些功能,所以我想在将来为Python创建自己的外部C模块。

Can anyone please tell me the steps required to successfully build a 64-bit Python extension in C? 谁能告诉我在C中成功构建64位Python扩展所需的步骤? I know Python, I know C but I don't know about Visual Studio or Windows specific developer matters. 我知道Python,我知道C,但我不了解Visual Studio或Windows特定的开发人员问题。 I tried to follow the official guide on the Python web site (http://docs.python.org/extending/windows.html#building-on-windows) using Visual Studio 2008 (which is the only commercial product available here) but even the most basic example would fail to build. 我尝试使用Visual Studio 2008(这是此处唯一可用的商业产品)遵循Python网站(http://docs.python.org/extending/windows.html#building-on-windows)上的官方指南,但即使是最基本的例子也无法建立。

I've successfully compiled C extensions for Python on 64-bit Windows before by running the following commands from the "Visual Studio 2008 x64 Win64 Command Prompt" in the top level directory of the source distribution of the extension: 我之前通过在扩展源代码分发的顶级目录中运行“Visual Studio 2008 x64 Win64命令提示符”中的以下命令,成功编译了64位Windows上的C扩展:

set DISTUTILS_USE_SDK=1
set MSSdk=1
python setup.py install

I'd use Shed Skin : Just download, unzip, run the init bat, and compile your Python code . 我使用Shed Skin :只需下载,解压缩,运行init bat,然后编译Python代码

If that doesn't work, and you can get Microsoft's C compiler environment working, try Cython . 如果这不起作用,并且您可以使Microsoft的C编译器环境正常工作,请尝试Cython This tutorial compares a normal Python extension with its generated C version. 本教程将普通Python扩展与其生成的C版本进行比较。 Updated excerpts: 更新的摘录:

c_prime.pyx: c_prime.pyx:

def calculate(long limit):
    cdef long current
    cdef long divisor
    primes = []
    divisor = 0
    for current in range(limit):
        previous = []
        for divisor in range(2, current):
            if current % divisor == 0:
                break
        if divisor == current - 1:
            primes.append(current)
    return primes

setup.py: setup.py:

from distutils.core import setup
from distutils.extension import Extension
from Cython.Distutils import build_ext

setup(
  name = 'PrimeTest',
  ext_modules=[
    Extension('c_prime', ['c_prime.pyx'])
    ],
  cmdclass = {'build_ext': build_ext}
)

compile: 编译:

python setup.py build_ext --inplace --compiler=msvc

test_prime.py: test_prime.py:

from timeit import Timer

t = Timer('c_prime.calculate(10000)', 'import c_prime')
reps = 5
print(sum(t.repeat(repeat=reps, number=1)) / reps)

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

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