在CentOS版本6.7(最终版)上导入lxml失败

时间:2015-11-20 04:56:04

标签: python gcc centos lxml

我将centos6.7上的python更新为python2.7.3,并将新版本作为默认版本。然后我使用sudo yum install python-lxml来安装python-lxml。它显示

package python-lxml-2.2.3-1.1.el6.x86_64 has been installed and is the newest,there is nothing to do

但是当我使用python(默认的2.7.3)pythonimport lxml时,会发生以下错误 ImportError: No module named lxml。但是当我使用像/usr/bin/python2.6import lxml这样的旧版python时,不会发生错误。 lxml适用于旧版本的python。

问题是什么?

我尝试了很多其他方法:

1。使用pip安装lxml sudo /usr/local/bin/pip install lxml,但发生错误

src/lxml/lxml.etree.c:237975: error:expected ‘)’ before ‘value’

Compile failed: command 'gcc' failed with exit status 1

cc -I/usr/include/libxml2 -I/usr/include/libxml2 -c 

/tmp/xmlXPathInitB69jpu.c -o tmp/xmlXPathInitB69jpu.o

/tmp/xmlXPathInitB69jpu.c:1:26: error:libxml/xpath.h:no such file or directory

Could not find function xmlCheckVersion in library libxml2. Is libxml2 installed?

error: command 'gcc' failed with exit status 1

Command /usr/bin/python -c "import setuptools;__file__='/home/zhoulipeng/software/lxml-3.4.4/build/lxml/setup.py';exec(compile(open(__file__).read().replace('\r\n', '\n'), __file__, 'exec'))" install --single-version-externally-managed --record /tmp/pip-VIpDJ1-record/install-record.txt failed with error code 1 4

似乎没有安装libxml2,所以我安装了所有依赖项sudo yum install libxml2-devel libxslt-devel python-devel,它显示:

package libxml2-devel-2.7.6-20.el6.x86_64 has been installed and is the newest
package libxslt-devel-1.1.26-2.el6_3.1.x86_64 has been installed and is the newest
package python-devel-2.6.6-64.el6.x86_64 has been installed and is the newest

这意味着我已经安装了libxml2包。

2. 从源代码

安装lxml

当我使用sudo python setup.py build构建源代码时,它失败了 src/lxml/lxml.etree.c:201156:error:expected ')'before'__pyx_f_4lxml_5etree_xslt_doc_loader'  error: command 'gcc' failed with exit status 1

有人能帮助我吗?谢谢。

1 个答案:

答案 0 :(得分:1)

yum在pythons 2.6路径中安装了lxml。您需要为python 2.7搜索rpm或构建自己的rpm。

那就是无论谁提供你的python 27,也为lxml构建一个rpm。

如果您通过redhat的软件集安装python27,您也可以尝试:

yum install python27-python-pip -y
scl enable python27 bash
pip install lxml

# python
Python 2.7.8 (default, Jun  9 2015, 19:27:51)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-11)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import lxml
>>>