简体   繁体   English

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

[英]building a python module on windows using ms compiler

I am trying to build the example that comes with the source distribution of python under PC\\example_nt我正在尝试在PC\\example_nt下构建python的源代码分发附带的示例

I copied example.c and setup.py to a directory C:\\mymod我将 example.c 和 setup.py 复制到目录 C:\\mymod

When I run C:\\Python27\\python.exe setup.py install I get the error....当我运行C:\\Python27\\python.exe setup.py install出现错误....

error: Unable to find vcvarsall.bat

I did some digging around in distutils and saw that it was going after version 9 of microsoft visual studio but I only have version 8. Apparently it tries to get version 9 because of what the python under C:\\Python27 was compiled with.我在 distutils 中进行了一些挖掘,发现它正在使用 microsoft visual studio 的第 9 版,但我只有第 8 版。显然它试图获得第 9 版,因为 C:\\Python27 下的 python 是用什么编译的。

I modified setup.py and put the following at the very top.我修改了 setup.py 并将以下内容放在最顶部。

from distutils import msvc9compiler
msvc9compiler.VERSION = 8.0

After doing this I was able to compile and got the following....这样做之后,我能够编译并得到以下内容....

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

Now when I run C:\\Python27\\python.exe and try to import example I get the following...现在,当我运行 C:\\Python27\\python.exe 并尝试import example我得到以下信息...

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

Did I do something wrong?我做错什么了吗? Is VS8 unsupported for creating Python 2.7 modules? VS8 不支持创建 Python 2.7 模块吗? What should I do?我该怎么办?

Ultimately I need to build bindings for some Windows C library so that I can use Python to extend some proprietary program instead of C. I have to use VS8 for creating the C extension.最终我需要为一些 Windows C 库构建绑定,以便我可以使用 Python 来扩展一些专有程序而不是 C。我必须使用 VS8 来创建 C 扩展。 So where does that leave me.那么这让我在哪里。

Advice please.请指教。

Thanks, ~Eric谢谢,〜埃里克

Generally speaking you have to build the python modules using the same version of VS as python was built with.一般来说,您必须使用与构建 python 相同的 VS 版本来构建 python 模块。 You have several options:您有多种选择:

  1. Use Python2.6, which i think is VS8 (or an even earlier version, I'm sure there was a change between 2.5 and 2.6)使用 Python2.6,我认为是 VS8(或者更早的版本,我确定 2.5 和 2.6 之间有变化)
  2. Use VS9.使用VS9。 I assume you can't because the proprietary library you are using was built with VS8.我认为你不能,因为你使用的专有库是用 VS8 构建的。 Same problem as is happening with python really.确实与python发生的问题相同。
  3. Create your bindings using ctypes .使用ctypes创建绑定。 This can be hard and its very easy to crash your program.这可能很难,而且很容易使您的程序崩溃。
  4. Build Python2.7 from source using VS8.使用 VS8 从源代码构建 Python2.7。 If you can't use Python2.6 for some reason then this is probably the best bet.如果由于某种原因您不能使用 Python2.6,那么这可能是最好的选择。

I'd recommend option 1 if it works.如果可行,我会推荐选项 1。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在Windows下使用SCons构建boost :: python模块 - Building boost::python module with SCons under Windows 使用 python 构建 MS Access 数据库 - Building an MS Access database using python Python:在Anaconda(Windows)中将C ++模块作为共享库而不是扩展库 - Python: Building a C++ Module in Anaconda (Windows) as a shared object and not as an extension 在哪里可以找到 MS Windows 的 Python `pwd` 模块? - Where I can find the Python `pwd` module for MS Windows? 在MS-Windows计算机上使用Python virtualenv - Using a Python virtualenv on a MS-Windows machine 使用带有Python的Windows上的符号链接模块 - Using a symlinked module on Windows with Python Python:在 Windows 上选择 C++ 编译器 — MS Visual Studio 到 Mingw-w64。 (PyStan 编译错误) - Python: Choosing C++ compiler on Windows — MS Visual Studio to Mingw-w64. (PyStan CompileError) 如何让64位Anaconda Python在Windows上找到Cython的MS C编译器? - How can I get 64-bit Anaconda Python to find the MS C compiler for Cython on Windows? 使用re模块帮助在python中构建正则表达式 - Help building a regular expression in python using the re module ModuleNotFoundError:在 macos 上使用 pyenv 构建 python 时没有名为“_lzma”的模块 - ModuleNotFoundError: No module named '_lzma' when building python using pyenv on macos
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM