在 Windows 上为 Python 2.7 构建 lxml

发布于 2024-09-05 09:08:35 字数 1191 浏览 2 评论 0原文

我正在尝试在 Windows 64 位计算机上为 Python 2.7 构建 lxml。我找不到 Python 2.7 版本的 lxml Egg。所以我从源头编译它。 说明进行操作

我按照此网站http://lxml.de/build.html

静态链接部分下的 。我收到错误

C:\Documents and Settings\Administrator\Desktop\lxmlpackage\lxml-2.2.6\lxml-2.2.
6>python setup.py bdist_wininst --static
Building lxml version 2.2.6.
NOTE: Trying to build without Cython, pre-generated 'src/lxml/lxml.etree.c' need
s to be available.
ERROR: 'xslt-config' is not recognized as an internal or external command,
operable program or batch file.

** make sure the development packages of libxml2 and libxslt are installed **

Using build configuration of libxslt
Building against libxml2/libxslt in one of the following directories:
  ..\libxml2-2.7.6--win32--w2k--x64\lib
  ..\libxslt-1.1.26--win32--w2k--x64--0002\lib
  ..\zlib-1.2.4--win32--w2k--x64
  ..\iconv-1.9.1--win32--w2k--x64-0001\lib
running bdist_wininst
running build
running build_py
running build_ext
building 'lxml.etree' extension
error: Unable to find vcvarsall.bat

有人可以帮我解决这个问题吗?我尝试设置 Microsoft Visual Studio 的路径.. 我可以从命令行运行 vcvarsall.bat..但是 python 有问题

I am trying to build lxml for Python 2.7 on Windows 64 bit machine. I couldn't find lxml egg for Python 2.7 version. So I am compiling it from sources. I am following instructions on this site

http://lxml.de/build.html

under static linking section. I am getting error

C:\Documents and Settings\Administrator\Desktop\lxmlpackage\lxml-2.2.6\lxml-2.2.
6>python setup.py bdist_wininst --static
Building lxml version 2.2.6.
NOTE: Trying to build without Cython, pre-generated 'src/lxml/lxml.etree.c' need
s to be available.
ERROR: 'xslt-config' is not recognized as an internal or external command,
operable program or batch file.

** make sure the development packages of libxml2 and libxslt are installed **

Using build configuration of libxslt
Building against libxml2/libxslt in one of the following directories:
  ..\libxml2-2.7.6--win32--w2k--x64\lib
  ..\libxslt-1.1.26--win32--w2k--x64--0002\lib
  ..\zlib-1.2.4--win32--w2k--x64
  ..\iconv-1.9.1--win32--w2k--x64-0001\lib
running bdist_wininst
running build
running build_py
running build_ext
building 'lxml.etree' extension
error: Unable to find vcvarsall.bat

Can any one help me with this? I tried setting the path to have Microsoft Visual Studio..
I can run vcvarsall.bat from the commandline.. but python is having problems

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

不回头走下去 2024-09-12 09:08:35

我敢打赌你不会为此使用 VS 2008 :)

def find_vcvarsall(version): 函数(猜猜看,它会查找 vcvarsall.bat),并在 distutils 中添加以下注释

首先它尝试寻找
注册表中VS 2008的productdir。
如果失败,它会退回到
VS90COMNTOOLS 环境变量。

如果您没有使用 VS 2008,那么您既没有注册表项,也没有合适的环境变量,这就是 distutils 找不到 vcvarsall.bat 文件的原因。它检查bat 文件是否可以通过PATH 环境变量访问。

解决方案是定义 VS90COMNTOOLS 变量指向 Visual Studio 的 Tools 目录。

话虽如此,请查看 11.4。 distutils.msvccompiler — Python 文档中的 Microsoft Compiler 部分,其中指出

通常,扩展模块需要
使用相同的编译器编译
用于编译 Python。

Martin 诉 Loewis,标题为 立即下载 Visual Studio Express 2008< /a> 在 python-list 邮件列表上的状态相同

Python 2.6、2.7 和 3.1 均已构建
随着该版本(即2008年)。因为
另一个悠久的传统,Python
扩展模块必须使用
相同的编译器版本(更多
具体来说,CRT 版本)作为 Python
本身。所以要构建扩展模块
对于任何这些版本,您需要
有 VS 2008 或 VS 2008 的副本
快递。

根据上述陈述,如果您想为 Python 2.7 构建 lxml,您应该使用 VS 2008,因此尽管设置 VS90COMNTOOLS 可以查找 vcvarsall.bat 文件,但它不是解决方案。

话虽这么说 :) 人们确实尝试使用较旧的 CRT 和较新的编译器:
我可以将 Visual Studio 2010 的 C++ 编译器与 Visual Studio 2008 的 C++ 运行时库一起使用吗?
如何强制 C++ 编译器使用特定的 CRT 版本?
VS 2008 - 链接到较旧的 C 运行时

我要感谢 Kev Dwyer(他指出了 VS 版本的重要性,它是使用)和 Stefan Behnel (将我指向 distutils 作为处理编译器配置的地方)在线程 在 Windows 下构建 lxml 时出现问题 - 错误:无法在 lxml 邮件列表上找到 vcvarsall.bat。我还要感谢 freenode #distutils IRC 频道的 agronholm 确认 distutils 确实包含查找 vcvarsall.bat 文件的代码。

I bet you're not using VS 2008 for this :)

There's def find_vcvarsall(version): function (guess what, it looks for vcvarsall.bat) in distutils with the following comment

At first it tries to find the
productdir of VS 2008 in the registry.
If that fails it falls back to the
VS90COMNTOOLS env var.

If you're not using VS 2008 then you have neither the registry key nor suitable environment variable and that's why distutils can't find vcvarsall.bat file. It does not check if the bat file is reachable through the PATH environment variable.

The solution is to define VS90COMNTOOLS variable to point to Tools directory of Visual Studio.

That being said take a look at 11.4. distutils.msvccompiler — Microsoft Compiler section in Python's docs which states

Typically, extension modules need to
be compiled with the same compiler
that was used to compile Python.

Martin v. Loewis in the email titled Download Visual Studio Express 2008 now on python-list mailing list states the same

Python 2.6, 2.7, and 3.1 are all built
with that release (i.e. 2008). Because
of another long tradition, Python
extension modules must be built with
the same compiler version (more
specifically, CRT version) as Python
itself. So to build extension modules
for any of these releases, you need to
have a copy of VS 2008 or VS 2008
Express.

In the light of above statements you should use VS 2008 if you want to build lxml for Python 2.7 so although setting VS90COMNTOOLS takes care of finding vcvarsall.bat file it's not the solution.

That being said :) people do try to use older CRT with newer compiler:
Can I use Visual Studio 2010's C++ compiler with Visual Studio 2008's C++ Runtime Library?
How to Enforce C++ compiler to use specific CRT version?
VS 2008 - Link against older C runtime

I'd like to thank Kev Dwyer (for pointing out importance of version of VS which is used) and Stefan Behnel (for pointing me to distutils as a place dealing with compiler's configuration) in the thread Problem building lxml under Windows - error: Unable to find vcvarsall.bat on lxml mailing list. I'd like to also thank agronholm from freenode #distutils IRC channel for confirmation that distutils does contain code which looks for vcvarsall.bat file.

子栖 2024-09-12 09:08:35

Jorj McKie 几乎是正确的:确实安装了 VCForPython27.msi还不够,是的,distutils 中存在一个问题,导致它无法找到 find_vcvarsall。事实上问题并不直接出在 distutils 上,而是出在 VCForPython27.msi 的打包方式以及 vcvarsall.bat 的放置位置(文件夹布局与 VS2008 SDK 不同)。

同时,一个简单的解决方法可能会在 Python 2.7.11 中得到修补:使用 setuptools 而不是 distutils。

如果您无法使用 distutils,还有另一个手动解决方法:

1) Enter MSVC for Python command prompt
2) SET DISTUTILS_USE_SDK=1
3) SET MSSdk=1
4) you can then build your C extensions: python.exe setup.py ...

Gregory Szorc 的错误报告和解决方法:
http://bugs.python.org/issue23246

更多信息和在内部使用 %%cython magic 的解决方法IPython: https://github.com/cython/cython/wiki/CythonExtensionsOnWindows

Jorj McKie was almost correct: indeed installing VCForPython27.msi isn't enough, and yes there is an issue in distutils which prevent it from finding find_vcvarsall. In fact the issue is not directly in distutils, but in how VCForPython27.msi was packaged and where vcvarsall.bat is placed (the folders layout is different from the VS2008 SDK).

A simple workaround meanwhile this gets patched maybe in Python 2.7.11: use setuptools instead of distutils.

Another manual workaround if you're stuck with distutils:

1) Enter MSVC for Python command prompt
2) SET DISTUTILS_USE_SDK=1
3) SET MSSdk=1
4) you can then build your C extensions: python.exe setup.py ...

Bug report and workaround by Gregory Szorc:
http://bugs.python.org/issue23246

More info and a workaround for using %%cython magic inside IPython: https://github.com/cython/cython/wiki/CythonExtensionsOnWindows

不知所踪 2024-09-12 09:08:35

按照推荐的解决方案进行操作后:

  1. 从 Microsoft 下载 VCForPython27.msi
  2. 安装它(Win7,Python(x, y) 2.7.9 32位),
  3. 输入/更新环境变量 VS90COMNTOOLS > 到
    安装目录值(C:\Program Files (x86)\Common
    Files\Microsoft\Visual C++ for Python\9.0)

我的问题仍然存在(想要用 C 构建 Python 扩展)。

在一切正常工作之前,我必须进行以下 2 个令人难以置信的肮脏调整:

  1. 修改 "C:\Python27\Lib\distutils" 中的 "msvc9compiler.py"
    函数find_vcvarsall,现在指向“Visual C++ for
    Python"
    而不是 "VC"
  2. 将目录Founder复制到"C:\Program Files (x86)\Common 下
    Files\Microsoft\Visual C++ for Python\9.0\"
    "C:\Program Files
    (x86)\Common Files\Microsoft\Visual C++ for Python\"
    (即一个目录
    等级提升)。

我无法判断谁在这里做错了什么 - 可能是我。

编辑。由于此 distutils bug 中描述的问题,移动目录有效。

即使设置了 VS90COMNTOOLS,msvc9compiler 也无法找到 vcvarsall.bat,因为它安装在 %installdir%/vcvarsall.bat 中,而不是 %installdir%/VC/vcvarsall .bat

所描述的解决方法是使用 Visual C++ 命令提示符:

  1. 输入 MSVC for Python 命令提示符

  2. 设置 DISTUTILS_USE_SDK=1

  3. 设置 MSSdk=1

  4. python.exe setup.py ...

After following the recommended solution:

  1. downloading VCForPython27.msi from Microsoft,
  2. installing it (Win7, Python(x, y) 2.7.9 32bit),
  3. entering / updating the environment variable VS90COMNTOOLS to
    the installation directory value (C:\Program Files (x86)\Common
    Files\Microsoft\Visual C++ for Python\9.0)

my problem still existed (want to build a Python extension in C).

I had to do the following 2 unbelievably dirty tweaks, before everything now is indeed working:

  1. modify "msvc9compiler.py" in "C:\Python27\Lib\distutils",
    function find_vcvarsall, to now point to "Visual C++ for
    Python"
    instead of to "VC".
  2. copy the directories founder under "C:\Program Files (x86)\Common
    Files\Microsoft\Visual C++ for Python\9.0\"
    to "C:\Program Files
    (x86)\Common Files\Microsoft\Visual C++ for Python\"
    (i.e. one dir
    level up).

I cannot tell who was doing something wrong here - probably I.

EDIT. Moving directories works because of the issue described in this distutils bug.

even if VS90COMNTOOLS is set, msvc9compiler isn't able to find vcvarsall.bat because it is installed in %installdir%/vcvarsall.bat and not %installdir%/VC/vcvarsall.bat

The described workaround is using the Visual C++ command prompt:

  1. Enter MSVC for Python command prompt

  2. SET DISTUTILS_USE_SDK=1

  3. SET MSSdk=1

  4. python.exe setup.py ...

愿得七秒忆 2024-09-12 09:08:35

https://github.com/develersrl/gccwinbinaries

我也遇到了类似的问题。除了使用安装向导和设置一个首选项之外,无需任何其他操作即可立即生效。

https://github.com/develersrl/gccwinbinaries

I had similar issues. This worked instantly without anything other than using an install wizard and setting one preference.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文