在Windows x64上编译Python模块 [英] Compiling Python modules on Windows x64

查看:968
本文介绍了在Windows x64上编译Python模块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开始一些项目的文字处理,我需要NumPy和NLTK。
这是我第一次知道 easy_install 以及如何将python的新模块编译到系统中。

I'm starting out some projects in words processing and I needed NumPy and NLTK. That was the first time I got to know easy_install and how to compile new module of python into the system.

我有Python 2.7 x64加上VS 11和VS 12.也Cygwin(最新的一个我猜)。
我可以看到在文件中编译使用VS,它寻找VS env与编译python代码的版本相同的版本,为什么?
当我硬编码11.0这是我的版本,numpy没有建立在几个奇怪的错误, vcvarsall (它找到 vcvarsall ,可能误用了它。)

I have Python 2.7 x64 plus VS 11 and VS 12. Also Cygwin (the latest one I guess). I could see in the file that compiles using VS that it looks for VS env with the same version as the one that compiled the python code, why? When I hardcoded 11.0 which is my version, numpy failed to build on several strange errors regarding vcvarsall (it found vcvarsall, probably misused it).

我不能在Windows上构建python二进制文件?
如果不是,我可以在Linux for Windows上交叉编译? (使用与Google SDK for Android SDK相同的方法)

Can't I build python binaries on Windows? If not, can I cross compile on Linux for Windows? (using the same method as Google for the Android SDK)

推荐答案

更新:以下 Zooba 提及,免费的x86和AMD64(x86-64)VC90 c-compilers for Python-2.7现在可以从Microsoft 获得。

Update: As Zooba mentions below, free x86 and AMD64 (x86-64) VC90 c-compilers for Python-2.7 are now available from Microsoft.

更新: Patch vcvarsall.bat 直接使用SDK v7.0中的x64编译器,在任何shell中使用 pip ,而不是使用SDK外壳和设置 DISTUTILS_USE_SDK MSSdk 环境变量。请参见修复vcvarsall.bat以使用v90安装Python-2.7 x64扩展,而不是sdk7

Update: Patch vcvarsall.bat to use x64 compilers from SDK v7.0 directly with pip in any shell instead of using SDK shell and setting DISTUTILS_USE_SDK and MSSdk environmental variables as in directions below. See Fix vcvarsall.bat to install Python-2.7 x64 extensions with v90 instead of sdk7.

tl; dr:使用Windows SDK v7.0编译器,打开SDK shell并调用

tl;dr: Use Windows SDK v7.0 compilers, open the SDK shell and call

C:\> setenv /release /x64
C:\> set DISTUTILS_USE_SDK=1
C:\> MSSdk=1

使用distutils构建Python 2.7.x扩展, pip easy_install 。请参阅 Python x64软件包扩展与pip,MSVC 2008 Express& SDK 7

to build Python 2.7.x extensions using distutils, pip or easy_install. See Python x64 Package Extensions with pip, MSVC 2008 Express & SDK 7.

注意:您可以安装使用来自Christoph Gohlke的Intel MKL进行Numpy优化。对于virtualenv,请尝试将二进制分发窗口安装程序转换为 wheel 文件,可以使用pip安装。从来源构建 NumPy 琐碎。它已成为一种可能性,最近推出和可用的 OpenBLAS ,一个 GotoBLAS 。有可用于Windows x86和x86-64的优化二进制文件,以及比GotoBLAS或< a href =http://math-atlas.sourceforge.net/> ATLAS 。

Note: You can install Numpy optimized using Intel MKL from Christoph Gohlke. For virtualenv's, try converting the binary distribution windows installer to a wheel file which can be installed with pip. Building NumPy from source is not trivial. It has become a possibility with the recent introduction and availability of OpenBLAS, a fork of GotoBLAS. There are optimized binaries available for Windows x86 and x86-64 as well as source which is relatively simpler to compile than GotoBLAS or ATLAS.

注意:在过去我没有建议从源代码编译NumPy,因为您必须为您的平台优化BLAS,这是非常耗时的,并且参考BLAS(FORTRAN)或CBLAS(C / C ++)实现性能相对较差。 NumPy也取决于 LAPACK ,这也取决于BLAS是构建NumPy的额外障碍。但是OpenBLAS二进制文件已经用LAPACK编译,所以这个障碍已经被删除。请参见 Carl Kleffner的静态MinGW-w64工具链在Windows上使用OpenBLAS构建NumPy - 他已解决了一些ABI不兼容性和问题链接到正确的Microsoft Visual C公共运行时库(对于Python-2.7, msvcr90.dll )。

Note: In the past I hadn't recommended compiling NumPy from source because you had to optimize BLAS for your platform which was very time-intensive, and the reference BLAS (FORTRAN) or CBLAS (C/C++) implementations were relatively low-performing. Also NumPy depends on LAPACK which also depends on BLAS an additional hurdle to building NumPy. However OpenBLAS binaries are already compiled with LAPACK, so this obstacle has already been removed. See Carl Kleffner's static MinGW-w64 toolchains to build NumPy with OpenBLAS on Windows - he has resolved some of the ABI incompatibilities and issues linking to the correct Microsoft Visual C Common Runtime library (msvcr90.dll for Python-2.7).

编译扩展的建议方法是使用相同的编译器来编译Python共享库[1-3]。 官方Python 2.7.x for Windows 是使用 Microsoft Visual C ++编译器版本9.0 [4](aka MSVC90或简单地VC90)从Microsoft Visual Studio 2008专业版,你可能可以免费获得来自 Microsoft DreamSpark 。显然,默认情况下不安装x64编译器,因此请确保它们与x86编译器一起安装。 注意:MS Visual Studio 2008可能不再可用,但是MS Visual Studio 2010将允许您使用MSVC90工具链(如果已安装),可以从Windows SDK7安装。

The recommended way to compile extensions is to use the same compiler used to compile the Python shared library [1-3]. The official Python 2.7.x for Windows was compiled using Microsoft Visual C++ Compilers version 9.0 [4] (aka MSVC90 or simply VC90) from Microsoft Visual Studio 2008 Professional, which you might be able to get for free from Microsoft DreamSpark. Evidentally the x64 compilers are not installed by default, so make sure they are installed along with the x86 compilers. Note: MS Visual Studio 2008 may no longer be available, but MS Visual Studio 2010 will let you use the MSVC90 toolchain if installed, which can be installed from Windows SDK7.

当你从命令行执行python解释器时,你可以通过查看标题来判断你的Python是什么版本。 : Python 2.7.3(默认,2012年4月10日,23:24:47)[MSC v.1500 64位(AMD64)]在win32上意味着它是用VS 2008 C ++构建的。你可以从源代码构建Python,但这也是一项艰巨的任务。一般来说,不建议混合编译器运行时,虽然在实践中你可能会发现他们仍然工作。

You can tell what version your Python was built with by looking at the header when you execute the python interpreter from a command line. e.g.: Python 2.7.3 (default, Apr 10 2012, 23:24:47) [MSC v.1500 64 bit (AMD64)] on win32 means that it was built with VS 2008 C++. You can build Python yourself from source, but this is also an arduous task. In general it is not recommended to mix compiler runtimes, although in practice you may find that they still work.

如果你有心设置使用 GNU GCC ,则您必须使用 mingw-w64 因为 MinGW 仅适用于本机x86应用程序,而不是AMD64 / x86-64。您需要

If you have your heart set on using GNU GCC, then you would have to use mingw-w64 because MinGW is only for native x86 applications, not AMD64/x86-64. You will need to

  • create a distutils config file to specify the mingw compiler,
  • remove the -mno-cygwin bug from distutils.cygwinccompiler.Mingw32ccompiler and
  • export a library definitions file using either pexports.exe from msys or gendef.exe from msys2 and make a static library libpython27.a using dlltool.exe (msys/msys2) (but the newest releases of Official Python for Windows already have this file in the Python27\lib folder, thanks!).

但是最后你仍然需要链接到python使用的运行时, code> msvcr90.dll ,因此您需要 Visual C ++ 2008可再发行组件注意:mingw-w64的Windows GCC使用 msvcrt.dll 这不等于 msvcr90.dll 或更高版本

However in the end you will still need to link against the runtime that python was built with, msvcr90.dll, so you will need Visual C++ 2008 redistributable. Note: Windows GCC from mingw-w64 uses msvcrt.dll which is not the same as msvcr90.dll or later.

如果您想要免费使用,可以使用 Microsoft Visual C ++ 2008 Express SP1 ,但您需要添加 Windows SDK 7 for .NET Frameworks 3.5 SP1 ,因为快捷版本拥有x64编译器。安装x64 C ++扩展与VS2008 Express& SDK 7与 cython网站(用于Windows x64扩展)上的示例非常相似。 注意:MS Visual Studio 2008 Express不再可用或不受支持。

If you want to do it for free you can use Microsoft Visual C++ 2008 Express SP1 but you will need to add the Windows SDK 7 for .NET Frameworks 3.5 SP1 because the express version does not have the x64 compilers. The procedure for installing x64 C++ extensions with VS2008 Express & SDK 7 are very similar to the those on the cython site for windows x64 extensions. Note: MS Visual Studio 2008 Express is no longer available or supported.

FYI:您不一定需要Visual Studio使用Microsoft编译器。它们可以免费使用适当的SDK包。 CL.EXE 是编译器可执行文件,但您必须设置通常由Autotools或其他工具(如CMAKE)配置的任何平台选项。 CMAKE适用于MSVC,但Autotools AFAIK不适用于MSVC,并且需要一些POSIX环境和实用程序,在Windows上可以在 MSYS

FYI: You do not necessarily need Visual Studio to build using Microsoft compilers. They are available free with the appropriate SDK package. CL.EXE is the compiler executable, but you will have to set whatever platform options that are normally configured by Autotools or some other tool such as CMAKE. CMAKE plays well with MSVC, but Autotools AFAIK doesn't work with MSVC and would require some POSIX environment and utilities which on Windows are available in MSYS.

使用distutils或setuptools的软件包,他们可以通过参考文档和wikis在各个地方发布的指示,使用Windows SDK 7编译扩展:

For many Python packages that use distutils or setuptools, they can compile extensions using Windows SDK 7 by following the directions that are posted in various places through the reference documentation and wikis:


  1. 开始菜单中选择所有程序,然后选择 Microsoft Windows SDK v7.0 并启动 CMD Shell

  2. 步骤1相当于在运行框中键入以下内容:开始菜单或命令提示符( C:\Windows \System32\cmd.exe ):

  1. From the Start Menu select All Programs then Microsoft Windows SDK v7.0 and start CMD Shell to open a command window optimized for Windows SDK.
  2. Step #1 is the equivalent of typing the following in the Run box from the Start Menu or from a Command Prompt (aka C:\Windows\System32\cmd.exe):

%COMSPEC% /E:ON /V:ON /K "%PROGRAMFILES%\Microsoft SDKs\Windows\v7.0\Bin\SetEnv.cmd"

注意: / E:ON 启用命令扩展, / V:ON 启用延迟的变量扩展,对于 SetEnv.cmd 到函数,否则你会收到一条消息,表明x64编译器没有安装,& c。

NOTE: The flags /E:ON, which enables command extensions, and /V:ON, which enables delayed variable expansion, are both necessary to for SetEnv.cmd to function, or you will get an message that the x64 compilers are not installed, &c.

c> setenv / Release / x64 ,它将专门为Windows 7 x64发行版设置SDK环境变量(vs debug或x86是默认值)。

Then type setenv /Release /x64 which will set the SDK environment variables specifically for Windows 7 x64 release (vs debug or x86 which are the default).


  • pip install pyyaml ntlk 这是推荐的方式, see ntlk ,但您必须拥有 setuptools pip

  • python setup.py install ,提取tarball

  • easy_install pyyaml ntlk 这是旧的方式和安装鸡蛋的唯一方法。

  • pip install pyyaml ntlk which is the recommended way, see ntlk, but you must have setuptools and pip installed.
  • python setup.py install for each downloaded, extracted tarball
  • easy_install pyyaml ntlk which is the old way and the only way to install eggs.

[1] 在Windows上构建C和C ++扩展

[2] distutils.msvccompiler - Microsoft Compiler

[3] Python开发指南:入门:Windows

[4] 这个python编译的visual studio的版本是什么

这篇关于在Windows x64上编译Python模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆