Python安装lxml(编译安装其依赖的库)

依赖说明:

lxml是libxml2、libxslt的PythonBinding,依赖于libxml2和libxslt。libxslt依赖于libxml2。

由于我们的程序可能要分发到别的机器运行,所以要提取出一个可独立拷贝走的运行时环境,假设该目录为/home/liheyuan/env

1、安装libxml2

wget http://xmlsoft.org/sources/libxml2-2.9.0.tar.gz
tar -xzvf libxml2-2.9.0.tar.gz
./configure --prefix=/home/liheyuan/env --without-python
make
make install

2、安装libxslt

wget http://xmlsoft.org/sources/libxslt-1.1.27.tar.gz
./configure --prefix=/home/liheyuan/env --without-crypto --without-python --with-libxml-prefix=/home/liheyuan/env/
make
make install

3、安装lxml

依赖的库都搞定了,终于轮到Python的Binding了。

我们假设Python已经通过编译安装的方式,放到了同样的目录下:/home/liheyuan/env

# 下载
wget http://pypi.python.org/packages/source/l/lxml/lxml-3.0.1.tar.gz#md5=0f2b1a063ab3b6b0944cbc4a9a85dcfa
tar -xzvf lxml-3.0.1.tar.gz
cd lxml-3.0.1
# 解压缩、编译
/home/liheyuan/env/bin/python ./setup.py build --with-xslt-config=/home/liheyuan/env/bin/xslt-config
/home/liheyuan/env/bin/python ./setup.py install

最后看下效果:

/home/liheyuan/env/bin/python
Python 2.7.3 (default, Oct 22 2012, 13:32:03) 
Type "help", "copyright", "credits" or "license" for more information.
>>> import lxml
>>> import lxml.html
>>> 报错.....

错误提示,提示etree.so依赖错误!

由于so是我们自己build的,且不在系统默认环境变量路径内,所以我们需要把path加到系统环境变量(so)路径内,如下:

vim ~/.bashrc
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/home/liheyuan/env/lib

下次重新登陆Terminal就可以了!

Leave a Reply

Your email address will not be published. Required fields are marked *