Building 64-bit C Python extensions on Windows

后端 未结 2 764
北恋
北恋 2020-12-13 06:55

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 W

相关标签:
2条回答
  • 2020-12-13 07:31

    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:

    set DISTUTILS_USE_SDK=1
    set MSSdk=1
    python setup.py install
    
    0 讨论(0)
  • 2020-12-13 07:42

    I'd use Shed Skin: Just download, unzip, run the init bat, and compile your Python code.

    If that doesn't work, and you can get Microsoft's C compiler environment working, try Cython. This tutorial compares a normal Python extension with its generated C version. Updated excerpts:

    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}
    )
    

    compile:

    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)
    
    0 讨论(0)
提交回复
热议问题