如何在 Windows 上安装 lxml

How to install lxml on Windows

我正在尝试在我的 Windows 8.1 笔记本电脑上安装 Python 3.4,但失败得很惨。

首先,我尝试了简单明了的解决方案:pip install lxml。但是,这没有用。内容如下:

Downloading/unpacking lxml
  Running setup.py (path:C:\Users\CARTE_~1\AppData\Local\Temp\pip_build_carte_000\lxml\setup.py) egg_info for package lxml
    Building lxml version 3.4.2.
    Building without Cython.
    ERROR: b"'xslt-config' is not recognized as an internal or external command,\r\noperable program or batch file.\r\n"
    ** make sure the development packages of libxml2 and libxslt are installed **

    Using build configuration of libxslt
    C:\Python34\lib\distutils\dist.py:260: UserWarning: Unknown distribution option: 'bugtrack_url'
      warnings.warn(msg)

    warning: no previously-included files found matching '*.py'
Installing collected packages: lxml
  Running setup.py install for lxml
    Building lxml version 3.4.2.
    Building without Cython.
    ERROR: b"'xslt-config' is not recognized as an internal or external command,\r\noperable program or batch file.\r\n"
    ** make sure the development packages of libxml2 and libxslt are installed **

    Using build configuration of libxslt
    building 'lxml.etree' extension
    C:\Python34\lib\distutils\dist.py:260: UserWarning: Unknown distribution option: 'bugtrack_url'
      warnings.warn(msg)
    error: Unable to find vcvarsall.bat
    Complete output from command C:\Python34\python.exe -c "import setuptools, tokenize;__file__='C:\Users\CARTE_~1\AppData\Local\Temp\pip_build_carte_000\lxml\setup.py';exec(compile(getattr(tokenize, 'open', open)(__file__).read().replace('\r\n', '\n'), __file__, 'exec'))" install --record C:\Users\CARTE_~1\AppData\Local\Temp\pip-l8vvrv9g-record\install-record.txt --single-version-externally-managed --compile:
    Building lxml version 3.4.2.

Building without Cython.

ERROR: b"'xslt-config' is not recognized as an internal or external command,\r\noperable program or batch file.\r\n"

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



Using build configuration of libxslt

running install

running build

running build_py

creating build

creating build\lib.win32-3.4

creating build\lib.win32-3.4\lxml

copying src\lxml\builder.py -> build\lib.win32-3.4\lxml

copying src\lxml\cssselect.py -> build\lib.win32-3.4\lxml

copying src\lxml\doctestcompare.py -> build\lib.win32-3.4\lxml

copying src\lxml\ElementInclude.py -> build\lib.win32-3.4\lxml

copying src\lxml\pyclasslookup.py -> build\lib.win32-3.4\lxml

copying src\lxml\sax.py -> build\lib.win32-3.4\lxml

copying src\lxml\usedoctest.py -> build\lib.win32-3.4\lxml

copying src\lxml\_elementpath.py -> build\lib.win32-3.4\lxml

copying src\lxml\__init__.py -> build\lib.win32-3.4\lxml

creating build\lib.win32-3.4\lxml\includes

copying src\lxml\includes\__init__.py -> build\lib.win32-3.4\lxml\includes

creating build\lib.win32-3.4\lxml\html

copying src\lxml\html\builder.py -> build\lib.win32-3.4\lxml\html

copying src\lxml\html\clean.py -> build\lib.win32-3.4\lxml\html

copying src\lxml\html\defs.py -> build\lib.win32-3.4\lxml\html

copying src\lxml\html\diff.py -> build\lib.win32-3.4\lxml\html

copying src\lxml\html\ElementSoup.py -> build\lib.win32-3.4\lxml\html

copying src\lxml\html\formfill.py -> build\lib.win32-3.4\lxml\html

copying src\lxml\html\html5parser.py -> build\lib.win32-3.4\lxml\html

copying src\lxml\html\soupparser.py -> build\lib.win32-3.4\lxml\html

copying src\lxml\html\usedoctest.py -> build\lib.win32-3.4\lxml\html

copying src\lxml\html\_diffcommand.py -> build\lib.win32-3.4\lxml\html

copying src\lxml\html\_html5builder.py -> build\lib.win32-3.4\lxml\html

copying src\lxml\html\_setmixin.py -> build\lib.win32-3.4\lxml\html

copying src\lxml\html\__init__.py -> build\lib.win32-3.4\lxml\html

creating build\lib.win32-3.4\lxml\isoschematron

copying src\lxml\isoschematron\__init__.py -> build\lib.win32-3.4\lxml\isoschematron

copying src\lxml\lxml.etree.h -> build\lib.win32-3.4\lxml

copying src\lxml\lxml.etree_api.h -> build\lib.win32-3.4\lxml

copying src\lxml\includes\c14n.pxd -> build\lib.win32-3.4\lxml\includes

copying src\lxml\includes\config.pxd -> build\lib.win32-3.4\lxml\includes

copying src\lxml\includes\dtdvalid.pxd -> build\lib.win32-3.4\lxml\includes

copying src\lxml\includes\etreepublic.pxd -> build\lib.win32-3.4\lxml\includes

copying src\lxml\includes\htmlparser.pxd -> build\lib.win32-3.4\lxml\includes

copying src\lxml\includes\relaxng.pxd -> build\lib.win32-3.4\lxml\includes

copying src\lxml\includes\schematron.pxd -> build\lib.win32-3.4\lxml\includes

copying src\lxml\includes\tree.pxd -> build\lib.win32-3.4\lxml\includes

copying src\lxml\includes\uri.pxd -> build\lib.win32-3.4\lxml\includes

copying src\lxml\includes\xinclude.pxd -> build\lib.win32-3.4\lxml\includes

copying src\lxml\includes\xmlerror.pxd -> build\lib.win32-3.4\lxml\includes

copying src\lxml\includes\xmlparser.pxd -> build\lib.win32-3.4\lxml\includes

copying src\lxml\includes\xmlschema.pxd -> build\lib.win32-3.4\lxml\includes

copying src\lxml\includes\xpath.pxd -> build\lib.win32-3.4\lxml\includes

copying src\lxml\includes\xslt.pxd -> build\lib.win32-3.4\lxml\includes

copying src\lxml\includes\etree_defs.h -> build\lib.win32-3.4\lxml\includes

copying src\lxml\includes\lxml-version.h -> build\lib.win32-3.4\lxml\includes

creating build\lib.win32-3.4\lxml\isoschematron\resources

creating build\lib.win32-3.4\lxml\isoschematron\resources\rng

copying src\lxml\isoschematron\resources\rng\iso-schematron.rng -> build\lib.win32-3.4\lxml\isoschematron\resources\rng

creating build\lib.win32-3.4\lxml\isoschematron\resources\xsl

copying src\lxml\isoschematron\resources\xsl\RNG2Schtrn.xsl -> build\lib.win32-3.4\lxml\isoschematron\resources\xsl

copying src\lxml\isoschematron\resources\xsl\XSD2Schtrn.xsl -> build\lib.win32-3.4\lxml\isoschematron\resources\xsl

creating build\lib.win32-3.4\lxml\isoschematron\resources\xsl\iso-schematron-xslt1

copying src\lxml\isoschematron\resources\xsl\iso-schematron-xslt1\iso_abstract_expand.xsl -> build\lib.win32-3.4\lxml\isoschematron\resources\xsl\iso-schematron-xslt1

copying src\lxml\isoschematron\resources\xsl\iso-schematron-xslt1\iso_dsdl_include.xsl -> build\lib.win32-3.4\lxml\isoschematron\resources\xsl\iso-schematron-xslt1

copying src\lxml\isoschematron\resources\xsl\iso-schematron-xslt1\iso_schematron_message.xsl -> build\lib.win32-3.4\lxml\isoschematron\resources\xsl\iso-schematron-xslt1

copying src\lxml\isoschematron\resources\xsl\iso-schematron-xslt1\iso_schematron_skeleton_for_xslt1.xsl -> build\lib.win32-3.4\lxml\isoschematron\resources\xsl\iso-schematron-xslt1

copying src\lxml\isoschematron\resources\xsl\iso-schematron-xslt1\iso_svrl_for_xslt1.xsl -> build\lib.win32-3.4\lxml\isoschematron\resources\xsl\iso-schematron-xslt1

copying src\lxml\isoschematron\resources\xsl\iso-schematron-xslt1\readme.txt -> build\lib.win32-3.4\lxml\isoschematron\resources\xsl\iso-schematron-xslt1

running build_ext

building 'lxml.etree' extension

C:\Python34\lib\distutils\dist.py:260: UserWarning: Unknown distribution option: 'bugtrack_url'

  warnings.warn(msg)

error: Unable to find vcvarsall.bat

----------------------------------------
Cleaning up...
Command C:\Python34\python.exe -c "import setuptools, tokenize;__file__='C:\Users\CARTE_~1\AppData\Local\Temp\pip_build_carte_000\lxml\setup.py';exec(compile(getattr(tokenize, 'open', open)(__file__).read().replace('\r\n', '\n'), __file__, 'exec'))" install --record C:\Users\CARTE_~1\AppData\Local\Temp\pip-l8vvrv9g-record\install-record.txt --single-version-externally-managed --compile failed with error code 1 in C:\Users\CARTE_~1\AppData\Local\Temp\pip_build_carte_000\lxml
Storing debug log for failure in C:\Users\carte_000\pip\pip.log

然后我查看了这个叫做 Internet 的伟大而有用的东西,很多人都有同样的错误需要 libxml2libxlst .他们推荐了一个叫 Christoph Gohlke 的人的页面,他在那里为一堆包提供了某种二进制的东西。你可以找到它 here (quicklink to the lxml part).

所以在我放弃尝试为 pip 寻找 libxml2 和 libxslt 之后,我决定去那里,并找到了 绝对吨 的下载。我知道我需要一个 64 位的,但我不知道我需要哪个“cp”。

因此,如果能给我一个关于 pip 方法或 Gohlke 索引方法的解决方案,那就太好了。

首先,根据评论,我下载了 lxml-3.4.2-cp34-none-win_amd64.whl 文件并尝试用 pip install 打开它,但它只是告诉我它不是我系统上的有效 wheel 文件或东西。

然后,我下载了 win_32 文件,它成功了!可能是因为我有 Intel 处理器,不出所料,AMD64 仅适用于 AMD 处理器。

或者你也可以去Christoph's Gohlke's Python page然后下载正确的lxml文件。 (通常因为我使用 python 3.4 并且我有一个 windows,我下载 lxml-3.4.4-cp34-none-win32.whl)

转到它所在的文件夹。单击后台(因此未选择任何内容),然后同时左移 + 右键单击​​并单击打开命令 window。然后输入 C:/Python34/Scripts/pip 安装“您刚 DOWNLOADED.whl 的文件名”

在这种情况下,它将是 C:/Python34/Scripts/pip install "lxml-3.4.4-cp34-none-win32.whl"

现在按回车键。这对我来说效果很好,在你输入报价后,你也可以按 Tab 键,它应该会为你填写正确的报价。

这些说明适用于 Windows7 或 Windows8 和 Python3.4

但是,它们应该适用于各种版本,如 python 的发布和其他各自的先决条件 change/evolve:

  1. 安装Python3.4
    1. 从下载页面下载 Python3.4 的最新版本 HERE
      • 直接 link for Win32 MSI 安装程序 -> HERE
    2. 只需 运行 要安装的 MSI python。它将在注册表中自行注册,并出现在 Add/REmove 程序中。
    3. 注意: 我接下来的说明假设您在 Python 安装向导
  2. C:\python33\C:\python34\scripts 文件夹添加到系统路径,方法是将这些目录添加到 控制面板 > 系统 > 高级系统中的 PATH 环境变量设置 link(高级选项卡)> 环境变量(按钮)
  3. 安装OpenSSL
    1. HERE 下载 Win32 OpenSSL 页面 以适合您的 Windows 版本和 PC 架构
    2. 下载 Visual C++ 2008 可再发行组件 适合您的Windows 版本和 PC 架构
    3. 下载 OpenSSL 适合您的 Windows 版本和体系结构(普通版本,不是轻量版本)
    4. c:\openssl-win32\bin(或类似)目录添加到您的 PATH,与您在上面添加 C:\python34C:\python34\scripts 的方式相同。
  4. 安装 Setuptoolsget-pip.py 应该为您安装 Setuptools),但是,以防万一...
    1. 下载 ez_setup.py HERE 并保存在 C:\python34\scripts
    2. 运行C:\python34\scripts> python ez_setup.py
  5. 安装PIP
    1. HERE 下载 get-pip.py 并将其保存在 C:\python34\scripts
    2. 运行 C:\python34\scripts> python get-pip.py
  6. 安装LXML
    1. HERE 下载 LXML 3.4.4 适合您的 Windows 版本和 PC 架构
    2. 运行 EXE文件

我最近也遇到了这个问题。 pip 无法处理 .whl 文件。我没有修复它,而是通过 .exe 文件安装它。您可以从 here 下载它。请选择正确的 python 版本。希望对您有所帮助。

貌似2018年的今天,lxml可以从PyPI安装:

C:\Users\Administrator>"c:\Program Files\Python37\python.exe" -m pip install -U pip wheel setuptools
Collecting pip
  Downloading https://files.pythonhosted.org/packages/c2/d7/90f34cb0d83a6c5631cf71dfe64cc1054598c843a92b400e55675cc2ac37/pip-18.1-py2.py3-none-any.whl (1.3MB)
    100% |████████████████████████████████| 1.3MB 3.3MB/s
Collecting wheel
  Downloading https://files.pythonhosted.org/packages/5a/9b/6aebe9e2636d35d1a93772fa644c828303e1d5d124e8a88f156f42ac4b87/wheel-0.32.2-py2.py3-none-any.whl
Collecting setuptools
  Downloading https://files.pythonhosted.org/packages/96/06/c8ee69628191285ddddffb277bd5abdf769166e7a14b867c2a172f0175b1/setuptools-40.4.3-py2.py3-none-any.whl (569kB)
    100% |████████████████████████████████| 573kB 2.9MB/s
Installing collected packages: pip, wheel, setuptools
  Found existing installation: pip 10.0.1
    Uninstalling pip-10.0.1:
      Successfully uninstalled pip-10.0.1
  The script wheel.exe is installed in 'c:\Program Files\Python37\Scripts' which is not on PATH.
  Consider adding this directory to PATH or, if you prefer to suppress this warning, use --no-warn-script-location.
  Found existing installation: setuptools 39.0.1
    Uninstalling setuptools-39.0.1:
      Successfully uninstalled setuptools-39.0.1
Successfully installed pip-18.1 setuptools-40.4.3 wheel-0.32.2

C:\Users\Administrator>pip install lxml
Collecting lxml
  Downloading https://files.pythonhosted.org/packages/d7/9d/1aa28aa9d293a816baec6c37328d6465b722b2ff3f4d1e93ed56e87813ee/lxml-4.2.5-cp37-cp37m-win_amd64.whl (3.6MB)
    100% |████████████████████████████████| 3.6MB 1.6MB/s
Installing collected packages: lxml
Successfully installed lxml-4.2.5

C:\Users\Administrator>python
Python 3.7.1 (v3.7.1:260ec2c36a, Oct 20 2018, 14:57:15) [MSC v.1915 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> from io import BytesIO
>>> some_file_or_file_like_object = BytesIO(b"<root>data</root>")
>>> from lxml import etree
>>> tree = etree.parse(some_file_or_file_like_object)
>>> etree.tostring(tree)
b'<root>data</root>'

我在列出 lxml==4.2.1 的需求文件中遇到了这个问题。但是,我能够通过将其更新为 lxml==4.2.5.

来解决问题

从分销商的网站上,正确的命令是:

pip install lxml==3.4.4

当然,更新版本号以匹配最新版本。

参考:https://pypi.org/project/lxml/3.4.4/

转到此页面 - https://www.lfd.uci.edu/~gohlke/pythonlibs/#lxml

下载与您机器兼容的libxml。 例如,在我的例子中,我安装了 64 位英特尔处理器 python 3.10。 所以,我已经下载了 lxml‑4.6.3‑cp310‑cp310‑win_amd64.whl .

然后 运行 -

pip install "download_path/lxml-4.6.3-cp310-cp310-win_amd64.whl"