2008-03-21

[]Python 2.5のdistutilsで、Visual Studio 2005を使いたい!

目的

Visual Studio 2005しかインストールしてない場合、Python 2.5だとdistutilsが正しく動かないっぽいので、どうにかしたいYO!

なんでVisual Studio 2005だと、distutilsが動かないんかな?

distutilsでVisual Studioを使うときのコンパイル環境は、sysモジュールのversionプロパティを参照して環境を選択をしているようです。Pythonインタプリタを起動して、以下のような命令を実行してみると、sys.versionプロパティが確認できます。

import sys;print sys.version

Python 2.5.2だと、以下のようになっています。Visual Studio 2003の環境が使われるようです。

2.5.2 (r252:60911, Feb 21 2008, 13:11:45) [MSC v.1310 32 bit (Intel)]

Python 2.6a1だと、以下のようになっています。Visual Studio 2008の環境が使われるようです。

2.6a1 (r26a1:61155, Mar  1 2008, 12:11:56) [MSC v.1500 32 bit (Intel)]

以上から察するに、Python 2.5.2とPython 2.6のどちらでも、Visual Studio 2005の環境が選択されることはないっぽいです。うーん、困った!

解決策

Python 2.6とVisual Studio 2008をインストールしよう!」というのを真っ先に思いつきましたが、あんまり環境を変えたくないんだよなー。というわけで、環境の変更を最低限に抑えてどうにかしてみました。簡単に言うと、distutilsだけの置き換えをしました。

Python 2.6 をインストールする

Python 2.6の公式ダウンロードページから、Windows版のインストーラーをダウンロードして、適当な場所にインストールしてください。

distutilsだけ置き換える

"Python-2.5.2/Lib/distutils"を別の場所に移動し、"Python-2.6a1/Lib/distutils"を"Python-2.5.2/Lib"以下にコピーしてください。以降はPython 2.6は必要ないので、アンインストールして構いません。

ファイルの修正
  • 例外をPython 2.5.2で許されている形式に変更。
  • get_build_version()関数で強制的に8を返すように変更。

上記の2点の変更を行います。distutilsディレクトリに、以下のパッチをあててください。

Index: msvccompiler.py
===================================================================
--- msvccompiler.py
+++ msvccompiler.py
@@ -170,6 +170,7 @@
     if majorVersion == 6:
         minorVersion = 0
     if majorVersion >= 6:
+        return 8
         return majorVersion + minorVersion
     # else we don't know what version of the compiler this is
     return None
Index: msvc9compiler.py
===================================================================
--- msvc9compiler.py
+++ msvc9compiler.py
@@ -128,7 +128,7 @@
                                "sdkinstallrootv2.0")
             else:
                 raise KeyError("sdkinstallrootv2.0")
-        except KeyError as exc: #
+        except KeyError, exc: #
             raise DistutilsPlatformError(
             """Python was built with Visual Studio 2008;
 extensions must be built with a compiler than can generate compatible binaries.
@@ -172,6 +172,7 @@
     if majorVersion == 6:
         minorVersion = 0
     if majorVersion >= 6:
+        return 8
         return majorVersion + minorVersion
     # else we don't know what version of the compiler this is
     return None
@@ -455,7 +456,7 @@
                 try:
                     self.spawn([self.rc] + pp_opts +
                                [output_opt] + [input_opt])
-                except DistutilsExecError as msg:
+                except DistutilsExecError, msg:
                     raise CompileError(msg)
                 continue
             elif ext in self._mc_extensions:
@@ -482,7 +483,7 @@
                     self.spawn([self.rc] +
                                ["/fo" + obj] + [rc_file])
 
-                except DistutilsExecError as msg:
+                except DistutilsExecError, msg:
                     raise CompileError(msg)
                 continue
             else:
@@ -495,7 +496,7 @@
                 self.spawn([self.cc] + compile_opts + pp_opts +
                            [input_opt, output_opt] +
                            extra_postargs)
-            except DistutilsExecError as msg:
+            except DistutilsExecError, msg:
                 raise CompileError(msg)
 
         return objects
@@ -520,7 +521,7 @@
                 pass # XXX what goes here?
             try:
                 self.spawn([self.lib] + lib_args)
-            except DistutilsExecError as msg:
+            except DistutilsExecError, msg:
                 raise LibError(msg)
         else:
             log.debug("skipping %s (up-to-date)", output_filename)
@@ -598,7 +599,7 @@
             self.mkpath(os.path.dirname(output_filename))
             try:
                 self.spawn([self.linker] + ld_args)
-            except DistutilsExecError as msg:
+            except DistutilsExecError, msg:
                 raise LinkError(msg)
 
         else:
manifestファイルを作る

python.exeと同じディレクトリに、以下のXMLを"python.exe.manifest"として保存してください。

<?xml version='1.0' encoding='UTF-8' standalone='yes'?>
<assembly xmlns='urn:schemas-microsoft-com:asm.v1' manifestVersion='1.0'>
  <dependency>
    <dependentAssembly>
      <assemblyIdentity
        type='win32'
        name='Microsoft.VC80.CRT'
        version='8.0.50608.0'
        processorArchitecture='x86'
        publicKeyToken='1fc8b3b9a1e18e3b'
        />
    </dependentAssembly>
  </dependency>
</assembly>

これで終わりです。

記事への反応(ブックマークコメント)

ログイン ユーザー登録
ようこそ ゲスト さん