如何在Linux上没有管理员权限的情况下安装Python的lxml模块?

5

我需要一些在主机上不存在的软件包(由于我和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 上说它有一些依赖关系。但是没有管理员权限怎么安装它们呢?


你在 virtualenv 中尝试过这个吗? - karantan
1个回答

11
如果您没有管理员权限,并且无法说服管理员为您安装相关软件包,那么您有两个选择: 选项1 - 下载libxml2和libxslt源文件,并在您的$HOME中编译和安装它们,然后使用这些副本构建python-lxml。
这是一个相当复杂的示例,因为如果您缺少进一步的依赖项,可能需要很长时间下载/编译。 选项2 - 下载适用于服务器上使用的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

我正在使用 Debian。谢谢,我会尝试的。 - Mikhail M.

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接