how to compile c-extensions under WinXP?

Scott David Daniels scott.daniels at acm.org
Tue Nov 8 16:33:35 EST 2005


Steve Holden wrote:
> severa at sophia.dtp.fmph.uniba.sk wrote:
> 
>> What should I do to be able to compile C-extensions (with python 2.4, 
>> winXP)? I get an error message, approximately "The .NET Framework SDK 
>> needs to be installed"; I tried to get something from the Microsoft 
>> web site, but maybe not the right version (or didn't set some 
>> variables), since the error remains. Could you please help me (it will 
>> need some patience with a computer newbie)?
>>
> See
> 
>   http://www.vrplumber.com/programming/mstoolkit
> 
> regards
>  Steve

I just got the following to work for me, maybe it will work for you.
I had some hassles with trying to get the patches to work and wanted a
technique which did not involve messing with the distribution.
It has the advantage of not actually writing into the main Python files.
You still need the free tools Mr. Fletcher links to, and a copy of
the "v7.bat" file that he talks about making (here I show it as in
C:/bin/v7.bat).

Here's an example setup:


     __version__ = '0.3'

     from distutils.core import setup, Extension

     for attempts in range(2):
         try:
             setup(name='simple', version='0.9',
                   ext_modules=[Extension('simple', ['simple.c'])])
         except SystemExit, e:
             print '*** Failed:', e.args[0]
         else:
             break   # Successful (avoid the retry)


         print 'Attempting to patch distutils.msvccompiler and retry:'

         <<patching code>>


=============
Patching code can either be a (properly indented) literal copy of the
following code, or you can simply write the following into a file
named "patch_msvc.py" and use "import patch_msvc" for patching code.


     import distutils.msvccompiler

     class BatReader(object):
         def __init__(self, batch_filename):
             self.source = open(batch_filename, 'rU')

         def getsets(self):
             '''Get all command lines which are "SET" commands.'''
             self.source.seek(0)
             for line in self.source:
                 try: command, rest = line.split(None, 1)
                 except ValueError: continue # Set a=b splits fine
                 if command.lower() == 'set':
                     try: key, value = rest.split('=', 1)
                     except ValueError: continue # breaking a=b;c;d
                 elif command.lower() == 'path':
                     key, value = command, rest #path x == set path=x
                 yield key, value.rstrip()

         def paths(self, pathkey):
             '''Find the file paths using our getsets methods.'''
             keyed = pathkey.upper()
             for key, paths in self.getsets():
                 if key.upper() == keyed:
                     elements = paths.split(';')
                     if elements[-1].upper() == keyed.join('%%'):
                         return elements[: -1]
                 return elements

     class MSVCCompiler(distutils.msvccompiler.MSVCCompiler):
         _patho = BatReader(r'C:\bin\vc7.bat') # or vcvars32.bat or ...
         def get_msvc_paths(self, path, platform='x86'):
             if path == 'library': path = 'lib'
             return self._patho.paths(path)

     distutils.msvccompiler.MSVCCompiler = MSVCCompiler
     # OK, patched it, now go try again.



-- 
-Scott David Daniels
scott.daniels at acm.org



More information about the Python-list mailing list