Weird scope error
Gary Herron
gherron at islandtraining.com
Sat Apr 5 12:09:00 EDT 2008
Rory McKinley wrote:
> Hi
>
> I am trying to use the TidyHTMLTreeBuilder module which is part of
> elementtidy, but I am getting what appears to be some sort of scope
> error and it is scrambling my n00b brain.
>
> The module file (TidyHTMLTreeBuilder.py) tried to import ElementTree by
> doing the following:
>
> from elementtree import ElementTree
>
> This bombed, so after a bit of poking around I replaced it with :
>
> from xml.etree import ElementTree
>
> This appears to have worked. However, when I try and parse a file using
> the function :
> TidyHTMLTreeBuilder.parse('weather_ct.html')
>
> I receive the following error:
>
> Traceback (most recent call last):
> File "<stdin>", line 1, in <module>
> File
> "/usr/lib/python2.5/site-packages/elementtidy/TidyHTMLTreeBuilder.py",
> line 107, in parse
> return ElementTree.parse(source, TreeBuilder())
> NameError: global name 'ElementTree' is not defined
>
>
> The code producing the error is as follows:
>
> def parse(source):
> return ElementTree.parse(source, TreeBuilder())
>
> Surely, if the from... import has worked, ElementTree is in the global
> scope and should therefore be accessible to the function parse?
>
Python has no such thing as this kind of a "global scope". (True, each
module has its own global scope, but that's not what you are talking
about.) So you'll have to fix the import for *every* module that needs
access to ElementTree. You might make the change as you mentioned
above for each, but really, I think you should just make ElementTree
directly importable by either installing it normally or including
.../xml/etree in your PYTHONPATH
Gary Herron
> Can anybody help?
>
> THanks
>
More information about the Python-list
mailing list