如何升级python2.6的lxml

时间:2013-12-06 17:47:10

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

运行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。

1 个答案:

答案 0 :(得分:4)

这适用于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

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

请注意,pip可执行文件是Python脚本,由某个Python安装程序运行。可能有一个pip使用许多Python安装,但由于硬盘空间相对便宜,大多数人为每个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'>