3

I have a problem when I try to execute:

myuser@my-pc:~$ python
Python 2.7.6 (default, Mar 22 2014, 22:59:56) 
[GCC 4.8.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import libxml2
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: No module named libxml2

I have installed python-dev libxml2 libxml2-dev libxslt-dev packages since I found on the internet a lot of such advices about these packages for my problem, but no result.

At the same time on my ubuntu desktop without any changes I can successfully do 'import libxml2' using python2.7 command line

Could you please advise how to fix this issue?

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
XZen
  • 225
  • 5
  • 27
  • 49
  • 1
    `libxml2` **is not a Python package**. Why are you trying to import it as such? What are you trying to do? – Martijn Pieters May 29 '14 at 09:55
  • Are you perhaps looking to [install the `lxml` library](http://stackoverflow.com/questions/6504810/how-to-install-lxml-on-ubuntu) instead? These days, there are also [Ubuntu packages](http://packages.ubuntu.com/search?keywords=python-lxml) for `lxml`. [`lxml`](http://lxml.de/) is a Python library that *uses* `libxml2` to parse XML documents. – Martijn Pieters May 29 '14 at 10:01
  • I just have existing code written by other and it works on one PC and doesn't work on another. I'm a new to python, but I'm surprised why on one PC this import works fine and on another PC it fails.. Also there are some info on the Internet about that, so I don't understand why this import is invalid: http://www.upfrontsystems.co.za/Members/hedley/my-random-musings/compile-and-install-libxml2-python could you please clarify? (edited before the next comment) – XZen May 29 '14 at 10:03
  • 1
    The `libxml2` Ubuntu package installs the *shared object library*; it is not a Python module. If your other PC has a Python module by the same name, you'll have to copy it over, there is no *published* Python module by that name. *However*, judging by the debian packages you installed, what you are really looking for is `lxml`. – Martijn Pieters May 29 '14 at 10:06
  • What do you mean by 'copy it over'? – XZen May 29 '14 at 10:08
  • 2
    What is the full error message when you try to launch the application (versus trying to import libxml2)? – Peter Gibson May 29 '14 at 10:11
  • @PeterGibson actually it's the same: `Traceback (most recent call last): File "/tmp/myscript.py", line 2, in import libxml2 ImportError: No module named libxml2` – XZen May 29 '14 at 10:13
  • @XZen: so what happens when you run `import libxml2` on the other machine? What does `print libxml2.__file__` tell you has been imported? – Martijn Pieters May 29 '14 at 10:21
  • @MartijnPieters here is output: `myuser@mypc:~$ python2.7 Python 2.7.4 (default, Sep 26 2013, 03:20:26) [GCC 4.7.3] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> import libxml2 >>> print libxml2.__file__ /usr/lib/python2.7/dist-packages/libxml2.pyc >>> ` – XZen May 29 '14 at 10:24
  • 2
    @XZen: I found this package: http://packages.ubuntu.com/search?keywords=python-libxml2 – Martijn Pieters May 29 '14 at 10:25
  • @MartijnPieters Really strange! I thought I've installed this one. So now the problem has gone. Thank you. – XZen May 29 '14 at 10:26

2 Answers2

3

The packages you've install do not include the Python bindings to libxml2. You've installed the shared object library as stated in the comments, but this is not available to Python programs without a Python module.

Installing python-dev libxml2 libxml2-dev libxslt-dev sounds like you're about to build the Python module from source, so see if the instructions you're following include other steps.

I believe this is the page for the module you need http://xmlsoft.org/python.html. See this SO answer for instructions on how to install it on OS-X that mostly apply to Linux also https://stackoverflow.com/a/9797504/66349

Community
  • 1
  • 1
Peter Gibson
  • 19,086
  • 7
  • 60
  • 64
3

You are trying to install the Shared Object libraries; these packages do not include Python bindings.

If the Python package on the other server is installed in /usr/lib/python2.7/dist-packages then that machine probably has python-libxml2 installed. It's included file list certainly matches what you have on the other machine:

/usr/lib/python2.7/dist-packages/drv_libxml2.py
/usr/lib/python2.7/dist-packages/libxml2.py
/usr/lib/python2.7/dist-packages/libxml2mod.so

For future reference, whenever you come across a Python library that you are not familiar with, you can inspect the module file path with:

import modulename
print modulename.__file__

If this is a Ubuntu package, you can search for the package it is part of with:

dpkg -S /path/to/file

but take into account that files ending in .pyc are bytecode caches; look for the .py equivalent instead.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343