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?

11
  • 1
    libxml2 is not a Python package. Why are you trying to import it as such? What are you trying to do? Commented May 29, 2014 at 9:55
  • Are you perhaps looking to install the lxml library instead? These days, there are also Ubuntu packages for lxml. lxml is a Python library that uses libxml2 to parse XML documents. Commented May 29, 2014 at 10:01
  • 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. Commented May 29, 2014 at 10:06
  • 2
    What is the full error message when you try to launch the application (versus trying to import libxml2)? Commented May 29, 2014 at 10:11
  • 2
    @XZen: I found this package: packages.ubuntu.com/search?keywords=python-libxml2 Commented May 29, 2014 at 10:25

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

4 Comments

how does one import modulename when getting the error ImportError: No module named modulename?
@keithpjolley: modulename is a placeholder here. Are you literally running import modulename?
no, it was a placeholder for me too. i couldn't understand how you could do what you said if you can't import in the first place.
@keithpjolley: I'm giving advice on modules you can import.
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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.