[使用ms编译器在Windows上构建python模块

问题描述 投票:1回答: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。显然,由于C:\ Python27下的python是用什么编译的,因此它试图获取版本9。

我修改了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.

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

最终,我需要为某些Windows C库构建绑定,以便可以使用Python扩展某些专有程序而不是C。我必须使用VS8来创建C扩展。那那把我留在哪里?

请咨询。

谢谢,〜Eric

python windows python-2.7 visual-studio distutils
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。

© www.soinside.com 2019 - 2024. All rights reserved.