How to install lxml for python without administative rights on linux?

笑着哭i 提交于 2019-11-29 04:51:49

If you have no admin rights, and cannot convince the administrator to install the relevant packages for you, you have two options:

Option 1 - Download sources for libxml2 and libxslt and compile and install them under your $HOME somewhere, then build python-lxml against those copies.

This is a pretty involved example, since if you're missing further dependencies you could be downloading / compiling for a long time.

Option 2 - Download the binary packages for the same distribution of Linux that is used on your server, and extract the contents under your home directory.

For example, if you're running Ubuntu Lucid, you'd first find out the version your OS is using and then download the packages you're missing:

% uname -m
x86_64
% aptitude show libxml2 | grep Version
Version: 2.7.6.dfsg-1ubuntu1.1

Next download the packages you need direct from the Ubuntu server:

% 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

Extract the contents and merge the lxml native and pure-python code and move the shared libraries to the top, then remove the extracted contents:

% 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

Finally, to use those files you need to set your LD_LIBRARY_PATH and PYTHONPATH environment variables to point into $HOME/root. Place these in your ~/.bashrc (or equivalent) so they are permanent:

% export LD_LIBRARY_PATH=$HOME/root/lib
% export PYTHONPATH=$HOME/root

You can verify that the shared objects are being found using ldd (if it's installed):

% 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)

Then you're ready to test Python:

% python
>>> from lxml import etree
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!