You are not logged in.

#1 2015-06-11 20:31:45

digitaldingo
Member
From: Denmark
Registered: 2011-03-28
Posts: 11
Website

[SOLVED] Python: "import libxml2" results in ImportError

I am trying to import libxml2 in python, but I get the following error:

$ python -c "import libxml2"
Traceback (most recent call last):
  File "<string>", line 1, in <module>
ImportError: dynamic module does not define init function (PyInit_libxml2)

And for python2 (no surprise here):

$ python2 -c "import libxml2"
Traceback (most recent call last):
  File "<string>", line 1, in <module>
ImportError: dynamic module does not define init function (initlibxml2)

I have tried to reinstall the following packages from the official repositories, but to no avail:

libxml2 
python-lxml 
python2-lxml 

I have had this problem for quite some time (at least half a year) and Google is not helping. Does anyone have any ideas on how to fix this? Thanks!

Last edited by digitaldingo (2015-06-12 15:57:49)

Offline

#2 2015-06-11 23:56:42

Scimmia
Fellow
Registered: 2012-09-01
Posts: 13,727

Re: [SOLVED] Python: "import libxml2" results in ImportError

Where exactly are you getting the py3 module? I see a py2 module included with libxml2, but the python{,2}-lxml packages have nothing to do with this.

Offline

#3 2015-06-12 10:25:17

digitaldingo
Member
From: Denmark
Registered: 2011-03-28
Posts: 11
Website

Re: [SOLVED] Python: "import libxml2" results in ImportError

Scimmia wrote:

Where exactly are you getting the py3 module? I see a py2 module included with libxml2, but the python{,2}-lxml packages have nothing to do with this.

Good question... I don't see anything related to python3.x either. How would I check where those files are? I can't see anything in site-packages nor does locate find anything.
While I guess this may point to the cause of my problem, I actually only need the python2 module (to build some packages). So I am fine with getting rid of the python3 module (wherever it may be).

Also, I mistook the python{,2}-lxml packages for providing the module based on their description. My bad. Thanks for pointing it out!

Offline

#4 2015-06-12 10:37:56

Raynman
Member
Registered: 2011-10-22
Posts: 1,539

Re: [SOLVED] Python: "import libxml2" results in ImportError

You can pass -v to see from where python is loading each module.

Offline

#5 2015-06-12 13:49:06

digitaldingo
Member
From: Denmark
Registered: 2011-03-28
Posts: 11
Website

Re: [SOLVED] Python: "import libxml2" results in ImportError

Raynman wrote:

You can pass -v to see from where python is loading each module.

But of course -- thanks! Importing using -vv I get the following for python2:

>>> import libxml2
# trying libxml2.so
# trying libxml2module.so
# trying libxml2.py
# trying libxml2.pyc
# trying /lib/libxml2.so
dlopen("/lib/libxml2.so", 2);
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: dynamic module does not define init function (initlibxml2)

For python3 (again with -vv):

>>> import libxml2
# trying /home/ks/libxml2.cpython-34m.so
# trying /home/ks/libxml2.abi3.so
# trying /home/ks/libxml2.so
# trying /home/ks/libxml2.py
# trying /home/ks/libxml2.pyc
# trying /lib/libxml2.cpython-34m.so
# trying /lib/libxml2.abi3.so
# trying /lib/libxml2.so
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<frozen importlib._bootstrap>", line 2237, in _find_and_load
  File "<frozen importlib._bootstrap>", line 2226, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 1191, in _load_unlocked
  File "<frozen importlib._bootstrap>", line 1161, in _load_backward_compatible
  File "<frozen importlib._bootstrap>", line 539, in _check_name_wrapper
  File "<frozen importlib._bootstrap>", line 1715, in load_module
  File "<frozen importlib._bootstrap>", line 321, in _call_with_frames_removed
ImportError: dynamic module does not define init function (PyInit_libxml2)

So I'm guessing that both versions of python try to load /lib/libxml2.so, but fail. /lib/libxml2.so is owned by libxml2 (as expected), and removing and reinstalling libxml2 does not fix the ImportError. Any other ideas on how to proceed from here?

Offline

#6 2015-06-12 15:21:46

Scimmia
Fellow
Registered: 2012-09-01
Posts: 13,727

Re: [SOLVED] Python: "import libxml2" results in ImportError

I'm guessing you've got a really strange $PYTHONPATH

Offline

#7 2015-06-12 15:56:55

digitaldingo
Member
From: Denmark
Registered: 2011-03-28
Posts: 11
Website

Re: [SOLVED] Python: "import libxml2" results in ImportError

Scimmia wrote:

I'm guessing you've got a really strange $PYTHONPATH

And indeed I did! For whatever reason I had prepended /lib to $PYTHONPATH. Removing that fixed the problem. Thanks a lot!

Offline

Board footer

Powered by FluxBB