25

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. 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.

Can anyone please tell me the steps required to successfully build a 64-bit Python extension in C? I know Python, I know C but I don't know about Visual Studio or Windows specific developer matters. 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.

Wayne Koorts
  • 10,861
  • 13
  • 46
  • 72
Alexandros
  • 4,425
  • 4
  • 23
  • 21
  • 5
    Add *how* even the most basic example fails. – Prof. Falken Jan 07 '11 at 10:42
  • 1
    For example: after following every direction, when I try to build example_nt I get the linker error that it cannot find python26.lib. I then tried using the python26.lib that is distributed with my version of python but that only resulted in two linker errors: 1>example.obj : error LNK2019: unresolved external symbol __imp___Py_NoneStruct referenced in function _ex_foo 1>example.obj : error LNK2019: unresolved external symbol __imp__Py_InitModule4 referenced in function _initexample – Alexandros Jan 07 '11 at 10:50
  • after that, I tried to build python26.lib myself by building the whole python 2.6 project from the source using VS2008 which resulted in a number of errors of course. However it produced a python26_d.lib which still gave a linker error when compiled along with example_nt – Alexandros Jan 07 '11 at 10:52
  • Building example_nt with "python setup.py build" should work out of the box on Python 2.6.6 AMD64 if you have installed Visual Studio 2008 Professional with the 64 bit compiler. – cgohlke Jan 07 '11 at 18:33
  • When running "python setup.py build" it gives me the error: raise ValueError(str(list(result.keys()))) ValueError: [u'path'] – Alexandros Jan 08 '11 at 16:40
  • 3
    Most likely you don't have the Visual Studio 2008 Professional installed correctly. The 64 bit compiler is an option that needs to be enabled at install time. In case you are using Visual Studio 2008 Express, this does not come with a 64 bit compiler. – cgohlke Jan 08 '11 at 17:56

2 Answers2

9

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
markshep
  • 728
  • 7
  • 17
2

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)
Cees Timmerman
  • 17,623
  • 11
  • 91
  • 124