【问题标题】:How can I upgrade lxml for python2.6如何为 python2.6 升级 lxml
【发布时间】:2013-12-24 04:22:13
【问题描述】:

我在运行 python 脚本时收到以下错误:

Traceback (most recent call last):
  File "/var/scripts/SchoolClosureManager/SchoolClosureManager.py", line 210, in <module>
    runnable.run()
  File "/var/scripts/SchoolClosureManager/SchoolClosureManager.py", line 18, in run
    reporter = SchoolClosureReporter(xml.name)
  File "/var/scripts/SchoolClosureManager/SchoolClosureManager.py", line 40, in __init__
    document = objectify.fromstring(xml, parser)
  File "lxml.objectify.pyx", line 1826, in lxml.objectify.fromstring (src/lxml/lxml.objectify.c:18625)
  File "lxml.etree.pyx", line 2532, in lxml.etree.fromstring (src/lxml/lxml.etree.c:48634)
  File "parser.pxi", line 1536, in lxml.etree._parseMemoryDocument (src/lxml/lxml.etree.c:72156)
ValueError: Unicode strings with encoding declaration are not supported.

我发现了这个错误报告:https://bugs.launchpad.net/lxml/+bug/683069,基本上说这是一个错误,并且一直是Fixed in trunk rev. 79947

我可以只更新lxml dist-package 吗?

我目前在 Ubuntu 8.04 上运行 python2.6.5。

【问题讨论】:

  • 我不同意那些投票结束这个问题的人,因为“关于通用计算硬件和软件的问题对于 Stack Overflow 来说是题外话除非它们直接涉及主要用于编程的工具 ”。 lxml 对我来说似乎是一个编程工具。

标签: python linux ubuntu command-line python-2.6


【解决方案1】:

这适用于 Ubuntu 11.10;我不确定它是否适用于 Ubuntu 8.04:

sudo apt-get install libxslt1-dev

如果您使用的是virtualenv(出于安全性和灵活性的考虑,我建议您这样做):

# with your virtual environment activated
pip install --upgrade lxml
pip install cssselect   # cssselect has been spun-off into its own project

如果不使用virtualenv,您可以卸载 Ubuntu 软件包 lxml:

sudo apt-get remove python-lxml

然后在系统范围内安装最新的lxml 包:

sudo pip install --upgrade lxml
sudo pip install cssselect

已更新以显示根据 cmets 中的讨论找到的安装方法。请注意,这并没有解决 OP 看到的 ValueError 异常;它只更新了 lxml 的版本。

请注意,pip 可执行文件是 Python 脚本,由特定的 Python 安装运行。可以在多个 Python 安装中使用单个 pip,但由于硬盘空间相对便宜,大多数人为每个 Python 安装安装一个 pip 可执行文件。

因此,如果您的系统上安装了多个 Python,请务必使用链接到目标 Python 可执行文件的 pip 可执行文件。

在 OP 的情况下

import sys; print(sys.executable)

返回

/usr/local/bin/python2.6

由于存在/usr/local/bin/pip

sudo /usr/local/bin/pip install --upgrade lxml
sudo /usr/local/bin/pip install cssselect

是适当的命令。


要测试您使用的是哪个版本的 lxml:

In [38]: import lxml.etree as ET

In [44]: ET.__version__
Out[44]: u'3.2.0'

In [45]: ET.LIBXML_COMPILED_VERSION
Out[46]: (2, 7, 8)

In [47]: ET.LIBXSLT_COMPILED_VERSION
Out[48]: (1, 1, 26)    

要检查您正在使用的模块的位置:

In [57]: import lxml.etree as ET

In [58]: ET
Out[58]: <module 'lxml.etree' from '/home/unutbu/.virtualenvs/dev/lib/python2.7/site-packages/lxml/etree.so'>

【讨论】:

  • 我能否知道我的 python 脚本正在运行哪个版本的 lxml?我在上面提供的链接显示了`/usr/lib/python2.6/dist-packages/lxml/etree.so`,这让我觉得我正在使用 Python 中包含的东西。
  • 当我运行sudo apt-get remove lxml 时,我收到错误Couldn't find package lxmllxmllibxml2 一样吗?
  • 糟糕——这个包被称为python-lxml,而不是lxmllibxml2python-lxml 使用的依赖项。
  • 我跑了sudo apt-get remove python-lxml。这是返回的内容:Reading package lists... Done Building dependency tree Reading state information... Done Package python-lxml is not installed, so not removed 0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.
  • 这些是我安装的唯一一个名称为“xml”的软件包libxml-parser-perl, libxml2, libxml2-dev, libxml2-utils, xml-core
猜你喜欢
  • 2011-06-06
  • 2017-07-18
  • 2016-01-27
  • 1970-01-01
  • 1970-01-01
  • 2016-12-21
  • 2018-08-01
  • 1970-01-01
  • 2016-12-01
相关资源
最近更新 更多