Python 如何在 Windows 上安装 lxml
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29440482/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to install lxml on Windows
提问by spelchekr
I'm trying to install lmxl
on my Windows 8.1 laptop with Python 3.4 and failing miserably.
我试图lmxl
在我的 Windows 8.1 笔记本电脑上安装Python 3.4,但失败了。
First off, I tried the simple and obvious solution: pip install lxml
. However, this didn't work. Here's what it said:
首先,我尝试了简单而明显的解决方案: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
So then I looked on this great and helpful thing called The Internetand a lot of people have the same error of needing libxml2
and libxlst
. They recommend a guy called Christoph Gohlke's page where he provides some sort of binary thingy for a bunch of packages. You can find it here(quicklink to the lxml part).
然后我看了这个叫做互联网的伟大而有用的东西,很多人都有同样的错误需要libxml2
和libxlst
。他们推荐了一个叫做 Christoph Gohlke 的页面,他为一堆包提供了某种二进制的东西。你可以在这里找到它(快速链接到 lxml 部分)。
So after I gave up on trying to find libxml2 and libxslt for pip, I decided to go there, and found an absolute tonof downloads. I know I need a 64-bit one, but I have no idea which "cp
" I need.
因此,在我放弃尝试为 pip 查找 libxml2 和 libxslt 之后,我决定去那里,并找到了大量下载。我知道我需要一个 64 位的,但我不知道cp
我需要哪个“ ”。
So an answer either giving me a solution on the pip
method or the Gohlke index method would be great.
因此,无论是给我一个关于该pip
方法的解决方案还是 Gohlke 指数方法的答案都会很棒。
采纳答案by spelchekr
First, following the comments, I downloaded the lxml-3.4.2-cp34-none-win_amd64.whl
file and tried to open it with a pip install
, but it just told me it wasn't a valid wheel file on my system or something.
首先,按照评论,我下载了lxml-3.4.2-cp34-none-win_amd64.whl
文件并尝试用 . 打开它pip install
,但它只是告诉我它不是我系统上的有效轮文件或其他东西。
Then, I downloaded the win_32
file and it worked! Maybe it's because I have an Intel processor and AMD64 is, unsurprisingly, only for AMD processors.
然后,我下载了该win_32
文件,并且成功了!也许是因为我有一个 Intel 处理器,而 AMD64 不出所料,只适用于 AMD 处理器。
回答by Kenny Truong
Or you can also go to Christoph's Gohlke's Python pageand then download the right lxml file. (Generally since I use python 3.4 and I have a windows, I download the lxml-3.4.4-cp34-none-win32.whl)
或者您也可以访问Christoph 的 Gohlke 的 Python 页面,然后下载正确的 lxml 文件。(通常因为我使用 python 3.4 并且我有一个 Windows,所以我下载了 lxml-3.4.4-cp34-none-win32.whl)
Go to the folder it is in. Click in the background (so nothing is selected), then leftshift + rightclick at the same time and click on open command window. Then type C:/Python34/Scripts/pip install "NAME OF THE FILE YOU JUST DOWNLOADED.whl"
转到它所在的文件夹。在背景中单击(因此没有选择任何内容),然后同时左移+右键单击并单击打开命令窗口。然后输入 C:/Python34/Scripts/pip install "NAME OF THE FILE YOU JUST DOWNLOADED.whl"
In this case it would be C:/Python34/Scripts/pip install "lxml-3.4.4-cp34-none-win32.whl"
在这种情况下,它将是 C:/Python34/Scripts/pip install "lxml-3.4.4-cp34-none-win32.whl"
Now hit enter. This worked well for me, and after you type the quote you can also press tab and it should fill the right one for you.
现在按回车键。这对我来说效果很好,在您输入报价后,您还可以按 Tab 键,它应该为您填写正确的报价。
回答by Flak DiNenno
These instructions are for Windows7 or Windows8 with Python3.4.
这些说明适用于带有 Python3.4 的 Windows7 或 Windows8。
However, they should work for various versions as the releases of python and other respective prerequisites change/evolve:
但是,它们应该适用于各种版本,因为 python 的版本和其他相应的先决条件发生了变化/发展:
- Install Python3.4:
- Download the last release of Python3.4 from the downloads page HERE
- Direct link for Win32 MSI installer -> HERE
- Simply run the MSI to install python. It will register itself in the registry, and appear in Add/REmove Programs.
- NOTE:my instructions that follow assume that you choose to install python to the default path of
C:\python34\
when asked during the Python Installation Wizard
- Download the last release of Python3.4 from the downloads page HERE
- Add the
C:\python33\
andC:\python34\scripts
folders to the system path by adding those directories to thePATH
environment variable from the Control Panel > System > Advanced System Settings link (Advanced Tab) > Environmental Variables (Button). - Install OpenSSL:
- Download Win32 OpenSSL pagefrom HEREfor your version of Windows and PC architecture
- Download Visual C++ 2008 redistributablesfor your version of Windows and PC architecture
- Download OpenSSLfor your version of Windows and architecture (the regular version, not the light one)
- Add the
c:\openssl-win32\bin
(or similar) directory to yourPATH
, the same way you addedC:\python34
andC:\python34\scripts
above.
- Install Setuptools(
get-pip.py
should install Setuptools for you), but, just in case...- Download
ez_setup.py
HEREand save it inC:\python34\scripts
- Run
C:\python34\scripts> python ez_setup.py
- Download
- Install PIP
- Download
get-pip.py
from HEREand save it inC:\python34\scripts
- Run
C:\python34\scripts> python get-pip.py
- Download
- Install LXML
- Download
LXML 3.4.4
from HEREfor your version of Windows and PC architecture - Run the EXE file
- Download
- 安装Python3.4:
- 通过从“控制面板”>“系统”>“高级系统设置”链接(高级选项卡)>“环境变量”(按钮)将这些目录添加到环境变量中,将
C:\python33\
和C:\python34\scripts
文件夹添加到系统路径中。PATH
- 安装OpenSSL:
- 从此处下载适用于您的 Windows 和 PC 架构版本的 Win32 OpenSSL 页面
- 下载适用于您的 Windows 和 PC 架构版本的Visual C++ 2008 可再发行组件
- 下载适用于您的 Windows 版本和体系结构的OpenSSL(常规版本,而不是轻型版本)
- 添加
c:\openssl-win32\bin
(或类似)目录到你PATH
,增加你以同样的方式C:\python34
和C:\python34\scripts
上面。
- 安装Setuptools(
get-pip.py
应该为您安装 Setuptools),但是,以防万一...- 在
ez_setup.py
此处下载并将其保存在C:\python34\scripts
- 跑
C:\python34\scripts> python ez_setup.py
- 在
- 安装画中画
get-pip.py
从这里下载并保存在C:\python34\scripts
- 跑
C:\python34\scripts> python get-pip.py
- 安装LXML
LXML 3.4.4
从这里下载适用于您的 Windows 和 PC 架构版本- 运行EXE文件
回答by Zik
I also meet this problem recently. pip can't not work on .whl file. Instead of fixing this, i install it through .exe file. you can download it from here.Please choose the right version of your python. Hope this can help you.
我最近也遇到这个问题。pip 不能处理 .whl 文件。我没有解决这个问题,而是通过 .exe 文件安装它。你可以从这里下载。请选择你的python的正确版本。希望这可以帮到你。
回答by George Sovetov
Looks like today in 2018, lxml
can be installed from PyPI:
看起来像 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>'
回答by Roger Iyengar
I had this issue with a requirements file that listed lxml==4.2.1. However, I was able to fix the problem by updating this to lxml==4.2.5.
我遇到了一个列出 lxml==4.2.1 的需求文件的问题。但是,我能够通过将其更新为 lxml==4.2.5 来解决该问题。
回答by Eli
From the distributer's website, the correct command is:
从分销商的网站上,正确的命令是:
pip install lxml==3.4.4
Of course, update version number to match the latest version.
当然,更新版本号以匹配最新版本。
Reference: https://pypi.org/project/lxml/3.4.4/