views:

252

answers:

3

The lxml package for Python seems to absolutely broken on my system. I am not sure of the problem, as all of the files are in place, it seems. My suspicion is that the problem is in __init__.py, but I don't have enough practice with the system to make an accurate diagnosis or fix the problem.

Here is some code that I think will help diagnose the problem:

Python 2.6.4 (r264:75706, Dec  7 2009, 18:45:15) 
[GCC 4.4.1] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import lxml
>>> dir(lxml)
['__builtins__', '__doc__', '__file__', '__name__', '__package__', '__path__']
>>> print lxml.__path__
['/usr/lib/python2.6/dist-packages/lxml']
>>> c = open("/usr/lib/python2.6/dist-packages/lxml/__init__.py", "r")
>>> for line in c:
...     print line
... 
# this is a package



>>> c.close()
>>> import os
>>> os.system("ls /usr/lib/python2.6/dist-packages/lxml/")
builder.py      ElementInclude.py   __init__.py    sax.pyc
builder.pyc     ElementInclude.pyc  __init__.pyc       usedoctest.py
cssselect.py        _elementpath.py objectify.so       usedoctest.pyc
cssselect.pyc       _elementpath.pyc    pyclasslookup.py
doctestcompare.py   etree.so        pyclasslookup.pyc
doctestcompare.pyc  html        sax.py
0
>>> 

Like I said, my suspicion is that __init__.py contains the problem, but I'm not 100% sure.

Also, I'm using Linux Mint 8 - the rough equivalent of Ubuntu 9.10.

Thanks in advance.

+1  A: 

I think all the lxml code is in subpackages. Try

from lxml import etree
Dietrich Epp
+1  A: 

It's perfectly normal for a __init__.py file to have nothing in it: http://docs.python.org/tutorial/modules.html#packages

The file is just there to let Python know that it's a package and not just a directory with a bunch of modules in it.

You just need to import the modules inside the package directly.

from lxml import etree, html

etc...

pib
+7  A: 

No, you're just doing it wrong! Try, e.g., from lxml import etree, and you should be able to use etree fully. import lxml -- importing the package! -- does not give you implicit access to any of the package's modules!-)

Alex Martelli
Aha - therein lies the rub. Thanks.
Reid