[英]Building 64-bit C Python extensions on Windows
我問這個問題是因為我需要構建一個特定的模塊(aspell_python, http ://wm.ite.pl/proj/aspell-python/)來處理在Windows 7上運行的64位Python 2.6(64當然是機器)。 我也一直想知道如何使用C代碼加速某些功能,所以我想在將來為Python創建自己的外部C模塊。
誰能告訴我在C中成功構建64位Python擴展所需的步驟? 我知道Python,我知道C,但我不了解Visual Studio或Windows特定的開發人員問題。 我嘗試使用Visual Studio 2008(這是此處唯一可用的商業產品)遵循Python網站(http://docs.python.org/extending/windows.html#building-on-windows)上的官方指南,但即使是最基本的例子也無法建立。
我之前通過在擴展源代碼分發的頂級目錄中運行“Visual Studio 2008 x64 Win64命令提示符”中的以下命令,成功編譯了64位Windows上的C擴展:
set DISTUTILS_USE_SDK=1
set MSSdk=1
python setup.py install
我使用Shed Skin :只需下載,解壓縮,運行init bat,然后編譯Python代碼 。
如果這不起作用,並且您可以使Microsoft的C編譯器環境正常工作,請嘗試Cython 。 本教程將普通Python擴展與其生成的C版本進行比較。 更新的摘錄:
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:
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}
)
編譯:
python setup.py build_ext --inplace --compiler=msvc
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.