1

我正在尝试在 PC\example_nt 下构建 python 的源代码分发附带的示例

我将 example.c 和 setup.py 复制到目录 C:\mymod

当我运行时,C:\Python27\python.exe setup.py install我得到错误....

error: Unable to find vcvarsall.bat

我在 distutils 中进行了一些挖掘,发现它是在 microsoft Visual Studio 的第 9 版之后运行的,但我只有第 8 版。显然它试图获取第 9 版,因为 C:\Python27 下的 python 是用什么编译的。

我修改了 setup.py 并将以下内容放在最顶部。

from distutils import msvc9compiler
msvc9compiler.VERSION = 8.0

这样做之后,我能够编译并得到以下内容....

C:\mymod>C:\Python27\python.exe setup.py install
running install
running build
running build_ext
building 'example' extension
creating build
creating build\temp.win32-2.7
creating build\temp.win32-2.7\Release
C:\Program Files\Microsoft Visual Studio 8\VC\BIN\cl.exe /c /nologo /Ox /MD /W3
/GS- /DNDEBUG -IC:\Python27\include -IC:\Python27\PC /Tcexample.c /Fobuild\temp.
win32-2.7\Release\example.obj
example.c
creating build\lib.win32-2.7
C:\Program Files\Microsoft Visual Studio 8\VC\BIN\link.exe /DLL /nologo /INCREME
NTAL:NO /LIBPATH:C:\Python27\libs /LIBPATH:C:\Python27\PCbuild /EXPORT:initexamp
le build\temp.win32-2.7\Release\example.obj /OUT:build\lib.win32-2.7\example.pyd
 /IMPLIB:build\temp.win32-2.7\Release\example.lib /MANIFESTFILE:build\temp.win32
-2.7\Release\example.pyd.manifest
   Creating library build\temp.win32-2.7\Release\example.lib and object build\te
mp.win32-2.7\Release\example.exp
C:\Program Files\Microsoft Visual Studio 8\VC\BIN\mt.exe -nologo -manifest build
\temp.win32-2.7\Release\example.pyd.manifest -outputresource:build\lib.win32-2.7
\example.pyd;2
running install_lib
copying build\lib.win32-2.7\example.pyd -> C:\Python27\Lib\site-packages
running install_egg_info
Removing C:\Python27\Lib\site-packages\example-1.0-py2.7.egg-info
Writing C:\Python27\Lib\site-packages\example-1.0-py2.7.egg-info

现在,当我运行 C:\Python27\python.exe 并尝试import example得到以下信息时...

ImportError: DLL load failed: The specified module could not be found.

我做错什么了吗?VS8 是否不支持创建 Python 2.7 模块?我应该怎么办?

最终我需要为一些 Windows C 库构建绑定,以便我可以使用 Python 来扩展一些专有程序而不是 C。我必须使用 VS8 来创建 C 扩展。那我怎么办。

请指教。

谢谢,~埃里克

4

1 回答 1

1

一般来说,您必须使用与构建 python 相同版本的 VS 构建 python 模块。你有几个选择:

  1. 使用 Python2.6,我认为是 VS8(或者更早的版本,我确信 2.5 和 2.6 之间有变化)
  2. 使用 VS9。我假设您不能,因为您使用的专有库是用 VS8 构建的。与 python 发生的问题相同。
  3. 使用ctypes创建绑定。这可能很难,而且很容易使您的程序崩溃。
  4. 使用 VS8 从源代码构建 Python2.7。如果由于某种原因您不能使用 Python2.6,那么这可能是最好的选择。

如果可行,我会推荐选项 1。

于 2010-11-18T19:31:54.550 回答