在Windows上为Python 2.7构建lxml

2024-05-14 15:19:21 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试在Windows64位计算机上为Python2.7构建lxml。我找不到Python2.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有问题


Tags: andoftopybuildliblxmlrunning
3条回答

我打赌你不会用VS2008来做这个:)

distutils中有一个def find_vcvarsall(version):函数(猜猜看,它查找的是vcvarsall.bat),带有以下注释

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.

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

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

这么说来看看Python文档中的11.4. distutils.msvccompiler — Microsoft Compiler部分

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

Martin v.Loewis在python list邮件列表中的标题为Download Visual Studio Express 2008 now的电子邮件中声明了相同的内容

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.

根据上述声明,如果您想为Python2.7构建lxml,应该使用VS2008,因此尽管设置VS90COMNTOOLS可以找到vcvarsall.bat文件,但它不是解决方案。

这就是说:)人们确实尝试使用旧的CRT和新的编译器:
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

我要感谢Kev Dwyer(指出所使用的VS版本的重要性)和Stefan Behnel(指出distutils是处理编译器配置的地方)在lxml邮件列表的线程Problem building lxml under Windows - error: Unable to find vcvarsall.bat中。我还要感谢freenode distutils IRC频道的agronholm确认distutils确实包含查找vcvarsall.bat文件的代码。

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

有关在IPython中使用%%cython magic的详细信息和解决方法:https://github.com/cython/cython/wiki/CythonExtensionsOnWindows

遵循建议的解决方案后:

  1. 正在从Microsoft下载VCForPython27.msi
  2. 安装(Win7,Python(x,y)2.7.9 32位)
  3. 输入/更新环境变量 安装目录值(C:\程序文件(x86)\公共 文件\Python \ 9的微软\Visual C++ + < <强>< /LI>

我的问题仍然存在(希望在C中构建一个Python扩展)。

我不得不做以下两个令人难以置信的肮脏的调整,现在一切都是真的工作:

  1. 在“C:\ Python27\Lib\distutils”中修改“msvc9compiler.py”, 函数<强> FordVC++ VALSULL ,现在指向 Python“而不是“VC”。
  2. 复制“C:\程序文件(x86)\公用”下的“方正”目录 文件\微软\ VisualC++对Python 9 \“ to <强>”c:\程序文件 (x86)\ Python“\<强”>的通用文件\\微软Visual C++(即DIR) 水平上升)。

我不知道是谁做错了什么-可能是我

编辑。由于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

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

  1. Enter MSVC for Python command prompt

  2. SET DISTUTILS_USE_SDK=1

  3. SET MSSdk=1

  4. python.exe setup.py ...

相关问题 更多 >

    热门问题