简体   繁体   English

如何升级适用于python2.6的lxml

[英]How can I upgrade lxml for python2.6

I am receiving the following error when running a python script: 运行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.

I have found this bug report: https://bugs.launchpad.net/lxml/+bug/683069 that basically says that it was a bug and has been Fixed in trunk rev. 79947 我发现了这个错误报告: https : //bugs.launchpad.net/lxml/+bug/683069 ,它基本上说这是一个错误,并且已经Fixed in trunk rev. 79947 Fixed in trunk rev. 79947

Am I able to just update the lxml dist-package? 我可以更新lxml dist-package吗?

I'm currently running python2.6.5 on Ubuntu 8.04. 我目前在Ubuntu 8.04上运行python2.6.5。

This worked with Ubuntu 11.10; 这与Ubuntu 11.10兼容。 I'm not sure if it will work with Ubuntu 8.04: 我不确定它是否可以与Ubuntu 8.04一起使用:

sudo apt-get install libxslt1-dev

If you are using virtualenv (which I recommend for safety and flexibility): 如果您使用的是virtualenv (出于安全性和灵活性考虑,建议您这样做):

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

If not using virtualenv , you could uninstall the Ubuntu package lxml: 如果不使用virtualenv ,则可以卸载Ubuntu软件包lxml:

sudo apt-get remove python-lxml

and then install the latest lxml package system-wide with: 然后使用以下命令在系统范围内安装最新的lxml软件包:

sudo pip install --upgrade lxml
sudo pip install cssselect

Updated to show the method of installation found per a discussion in the comments. 已更新以显示根据评论中的讨论找到的安装方法。 Note that this did not solve the ValueError exception the OP was seeing; 注意,这并不能解决OP看到的ValueError异常。 it only updated the version of lxml. 它仅更新了lxml的版本。

Note that the pip executable is a Python script, run by a certain installation of Python. 请注意, pip可执行文件是Python脚本,由特定的Python安装程序运行。 It might be possible to use a single pip with many Python installations, but since hard drive space is relatively cheap, most people install one pip executable for each Python installation. 可能在多个Python安装中使用一个pip ,但是由于硬盘空间相对便宜,因此大多数人为每个Python安装安装一个pip可执行文件。

So if you have multiple Python installations on your system, it is important to use the pip executable which is linked to your target Python executable. 因此,如果系统上安装了多个Python,则使用链接到目标Python可执行文件的pip可执行文件很重要。

In the OP's case 在OP的情况下

import sys; print(sys.executable)

returned 回来

/usr/local/bin/python2.6

Since a /usr/local/bin/pip existed, 由于存在/usr/local/bin/pip

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

were the appropriate commands. 是适当的命令。


To test which version of lxml you are using: 要测试您使用的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)    

To check the location of the module you are using: 要检查所用模块的位置:

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'>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM