如何在Linux上无管理权限安装lxml?
我需要一些在主机上没有的包(而且我和Linux的关系……我们没怎么相处过)。
我以前是这样安装它们的:
# from the source
python setup.py install --user
或者
# with easy_install
easy_install prefix=~/.local package
但是用lxml的时候不行。我在构建的时候遇到了很多错误:
x:~/lxml-2.3$ python setup.py build
Building lxml version 2.3.
Building without Cython.
ERROR: /bin/sh: xslt-config: command not found
** make sure the development packages of libxml2 and libxslt are installed **
Using build configuration of libxslt
running build
running build_py
running build_ext
building 'lxml.etree' extension
gcc -pthread -fno-strict-aliasing -DNDEBUG -g -fwrapv -O2 -Wall -Wstrict-prototypes -fPIC -I/usr/include/python2.6 -c src/lxml/lxml.etree.c -o build/temp.linux-i686-2.6/src/lxml/lxml.etree.o -w
In file included from src/lxml/lxml.etree.c:227:
src/lxml/etree_defs.h:9:31: error: libxml/xmlversion.h: No such file or directory
src/lxml/etree_defs.h:11:4: error: #error the development package of libxml2 (header files etc.) is not installed correctly
src/lxml/etree_defs.h:13:32: error: libxslt/xsltconfig.h: No such file or directory
src/lxml/etree_defs.h:15:4: error: #error the development package of libxslt (header files etc.) is not installed correctly
src/lxml/lxml.etree.c:230:29: error: libxml/encoding.h: No such file or directory
src/lxml/lxml.etree.c:231:28: error: libxml/chvalid.h: No such file or directory
src/lxml/lxml.etree.c:232:25: error: libxml/hash.h: No such file or directory
...
src/lxml/lxml.etree.c:55179: error: Б─≤xmlNodeБ─≥ undeclared (first use in this function)
src/lxml/lxml.etree.c:55179: error: Б─≤__pyx_v_c_nodeБ─≥ undeclared (first use in this function)
src/lxml/lxml.etree.c:55184: error: Б─≤_node_to_node_functionБ─≥ undeclared (first use in this function)
src/lxml/lxml.etree.c:55184: error: expected Б─≤;Б─≥ before Б─≤__pyx_v_next_elementБ─≥
src/lxml/lxml.etree.c:55251: error: Б─≤struct __pyx_obj_4lxml_5etree__ReadOnlyProxyБ─≥ has no member named Б─≤_c_nodeБ─≥
...
http://lxml.de/installation.html上说它有一些依赖项。但是没有管理员权限怎么安装这些依赖呢?
1 个回答
11
如果你没有管理员权限,也说服不了管理员为你安装相关的软件包,那你有两个选择:
选择一 - 下载libxml2
和libxslt
的源代码,然后在你的$HOME
目录下编译并安装它们,接着根据这些版本来构建python-lxml。
这个过程比较复杂,因为如果你还缺少其他依赖项,可能需要花很长时间来下载和编译。
选择二 - 下载与你的服务器上使用的Linux版本相同的二进制软件包,并将内容解压到你的主目录下。
举个例子,如果你使用的是Ubuntu Lucid,你首先需要查明你的操作系统版本,然后下载你缺少的软件包:
% uname -m
x86_64
% aptitude show libxml2 | grep Version
Version: 2.7.6.dfsg-1ubuntu1.1
接下来,从Ubuntu服务器直接下载你需要的软件包:
% mkdir root ; cd root
% wget http://us.archive.ubuntu.com/ubuntu/pool/main/libx/libxml2/libxml2_2.7.6.dfsg-1ubuntu1.1_amd64.deb
% wget http://us.archive.ubuntu.com/ubuntu/pool/main/libx/libxslt/libxslt1.1_1.1.26-6build1_amd64.deb
% wget http://us.archive.ubuntu.com/ubuntu/pool/main/l/lxml/python-lxml_2.2.4-1_amd64.deb
解压内容后,将lxml的本地代码和纯Python代码合并,并把共享库放到最上面,然后删除解压后的内容:
% dpkg-deb -x libxml2_2.7.6.dfsg-1ubuntu1.1_amd64.deb .
% dpkg-deb -x libxslt1.1_1.1.26-6build1_amd64.deb .
% dpkg-deb -x python-lxml_2.2.4-1_amd64.deb .
% mv ./usr/lib/python2.6/dist-packages/lxml .
% mv ./usr/share/pyshared/lxml/* lxml
% mv ./usr/lib .
% rm *.deb
% rm -rf usr
最后,为了使用这些文件,你需要设置你的LD_LIBRARY_PATH和PYTHONPATH环境变量,让它们指向$HOME/root
。把这些设置放到你的~/.bashrc
(或类似文件)中,这样它们就会一直有效:
% export LD_LIBRARY_PATH=$HOME/root/lib
% export PYTHONPATH=$HOME/root
你可以使用ldd
(如果已安装)来验证共享对象是否被找到:
% ldd $HOME/root/lxml/etree.so | grep $HOME
libxslt.so.1 => /home/user/root/lib/libxslt.so.1 (0x00007ff9b1f0f000)
libexslt.so.0 => /home/user/root/lib/libexslt.so.0 (0x00007ff9b1cfa000)
libxml2.so.2 => /home/user/root/lib/libxml2.so.2 (0x00007ff9b19a9000)
然后你就可以准备测试Python了:
% python
>>> from lxml import etree