Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

python

linux

lxml

I just need some packages which dont present at the host machine (and I and linux... we... we didn't spend much time together...).

I used to install them like:

# from the source
python setup.py install --user

or

# with easy_install
easy_install prefix=~/.local package

But it doesn't work with lxml. I get a lot of errors during the build:

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 says that it has some dependencies. But how to install them without administrative rights?

like image 484
Mikhail M. Avatar asked May 12 '11 09:05

Mikhail M.


People also ask

Is lxml included in Python?

lxml has been downloaded from the Python Package Index millions of times and is also available directly in many package distributions, e.g. for Linux or macOS.


1 Answers

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
like image 80
samplebias Avatar answered Sep 30 '22 09:09

samplebias