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

查看:51
本文介绍了在 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 编译的文件中看到它寻找与编译 python 代码版本相同的 VS env,为什么?当我对我的版本 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 上交叉编译吗?(Android SDK 使用与 Google 相同的方法)

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)

推荐答案

更新: 这个答案已经 5 年多了!Python-2.7 即将被弃用,因此您真正需要的是用于 Python-3 的 Microsoft Visual C 编译器.看看 MS Windows 编译器上的这个 Python wiki.带有 VC-14.0 的 MS Build Tools 2015 是为 Python-3.5 和 3.6 构建扩展所需要的.您可以安装较旧的 MS 构建工具 2015其中包括 VC-14.0 或更新的 2017 年的 MS 构建工具 - 单击链接,然后向下滚动,直到找到 Build Tools for Visual Studio2017 - 其中还包括 VC-14.0.

Update: This answer is now over 5 years old! Python-2.7 is about to be deprecated, so what you really need is a Microsoft Visual C compiler for Python-3. Take a look at this Python wiki on MS Windows Compilers. MS Build Tools 2015 with VC-14.0 is what you need to build extensions for Python-3.5 and 3.6. You can either install the older MS build tools 2015 which includes VC-14.0 or the newer MS build tools for 2017 - click the link, then scroll down until you find Build Tools for Visual Studio 2017 - which also includes VC-14.0.

此外,如果您的 pip 和 setuptools 版本是最新的,那么您可以忽略所有那些愚蠢的老式 MSSDK 废话.特别是如果您使用 VC for Python 2.7 或 MS 构建工具 2015.自 setuptools-24 以来,它只知道在哪里寻找这些编译器,希望如此.

更新:正如 Zooba 在下面提到的,适用于 Python-2.7 的免费 x86 和 AMD64 (x86-64) VC90 c 编译器现已可用来自微软.

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

更新: 补丁vcvarsall.bat 在任何 shell 中直接使用 SDK v7.0 的 x64 编译器和 pip,而不是使用 SDK shell 并设置 DISTUTILS_USE_SDKMSSdk 环境变量,如下所示.请参阅 修复 vcvarsall.bat 以安装 Python-2.7使用 v90 而不是 sdk7 的 x64 扩展.

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、pipeasy_install 构建 Python 2.7.x 扩展.参见 Python x64 Package Extensions with pip, MSVC 2008快递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.

注意:您可以安装 Numpy 优化使用Christoph Gohlke 的英特尔 MKL.对于 virtualenv,尝试将二进制分发 Windows 安装程序转换为 wheel 文件,该文件可以通过 pip 安装.从源代码构建NumPy并非微不足道.随着最近 OpenBLAS(GotoBLAS.有 适用于 Windows x86 和 x86-64 的优化二进制文件 以及相对简单的源代码编译比 GotoBLAS 或 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].Windows 的官方 Python 2.7.x 是使用 来自 Microsoft Visual Studio 2008 Professional 的 Microsoft Visual C++ 编译器版本 9.0 [4](又名 MSVC90 或简称 VC90),您可以从 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 是用哪个版本构建的.eg: Python 2.7.3 (default, Apr 10 2012, 23:24:47) [MSC v.1500 64 bit (AMD64)] on 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 Python27lib folder, thanks!).

但是,最终您仍然需要链接到构建 python 的运行时 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 因为 express 版本没有具有 x64 编译器.使用 VS2008 Express & 安装 x64 C++ 扩展的过程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.

仅供参考:您不一定需要使用 Microsoft 编译器来构建 Visual Studio.它们可通过相应的 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 的 Python 包,他们可以按照参考文档和维基在不同地方发布的说明使用 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打开一个针对 Windows SDK 优化的命令窗口.
  2. 第 1 步相当于在 开始菜单 或命令提示符(又名 C:WindowsSystem32cmd.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:WindowsSystem32cmd.exe):

%COMSPEC% /E:ON /V:ON /K "%PROGRAMFILES%Microsoft SDKsWindowsv7.0BinSetEnv.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.

然后输入 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 这是推荐的方式,参见 ntlk,但你必须有 setuptoolspip 已安装.
  • python setup.py install 每个下载的、解压的 tarball
  • easy_install pyyaml ntlk 这是安装eggs的老方法,也是唯一的方法.
  • 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 — 微软编译器
[3] Python 开发指南:入门:Windows
[4] 这个python编译的是什么版本的visual studio与

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

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