By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,319 Members | 2,361 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,319 IT Pros & Developers. It's quick & easy.

import statement / ElementTree

P: n/a
O/S: Windows 2K
Vsn of Python: 2.4

Currently:

1) Folder structure:

\workarea\ <- ElementTree files reside here
\xml\
\dom\
\parsers\
\sax\

2) The folder \workarea\ is in the path.

3) A script (which is working) makes calls to the Element(),
SubElement(), tostring() and XML() methods within ElementTree.py; the
script is organized as follows:

# top of file; not within any function/mehtod
import ElementTree

<examples of various calls within the module>

root = ElementTree.Element('request')
pscifinq = ElementTree.SubElement(root, 'pscifinq')
bank = ElementTree.SubElement(pscifinq, 'bank')
bank.text = '1'
inquiryString = ElementTree.tostring(root)

4) the term 'ElementTree files' referenced above refers to the
following files:
__init__.py (this file contains only comments)
ElementInclude.py
ElementPath.py
ElementTree.py
HTMLTreeBuilder.py
SgmlopXMLTreeBuilder.py
SimpleXMLTreeBuilder.py
SimpleXMLWriter.py
TidyHTMLTreeBuilder.py
TidyTools.py
XMLTreeBuilder.py

Want to change things as follows:

Folder structure:

\workarea\ <- ElementTree files no longer here
\xml\
\dom\
\elementtree\ <- ElementTree files reside here
\parsers\
\sax\

I tried changing the

import ElementTree

statement to:

import xml.elementtree.ElementTree

The result of changing the folder structure and the import statement
was the following error:

import xml.elementtree.ElementTree

ImportError: No module named elementtree.ElementTree

I verified that the file ElementTree.py really does reside in the
\workarea\xml\elementtree\ folder. Assuming that I really want the
ElementTree files to reside in the \workarea\xml\elementtree\ folder,
what changes must I make such that the script can locate the
ElementTree.py file? I have a hunch that there is something obvious
that I am missing in the import statement; is it possible to accomplish
this by changing only the import statement rather than changing each of
the calls to the Element(), SubElement(), XML() and tostring() methods.

Nov 5 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
mi************@yahoo.com wrote:
O/S: Windows 2K
Vsn of Python: 2.4

Currently:

1) Folder structure:

\workarea\ <- ElementTree files reside here
\xml\
\dom\
\parsers\
\sax\
First point, XML DOM comes packaged with Python 2.4. (IIRC, Python XML
is, or was, a seperate project that made it into the Python
distribution--maybe it still makes its own releases. I presume you're
using one of those release in lieu of the version supplied with Python.
I only point this out in case you didn't realize the xml package is in
Python 2.4. My apologies if this comes across as presumptuous of me.)

2) The folder \workarea\ is in the path.

3) A script (which is working) makes calls to the Element(),
SubElement(), tostring() and XML() methods within ElementTree.py; the
script is organized as follows:

# top of file; not within any function/mehtod
import ElementTree

<examples of various calls within the module>

root = ElementTree.Element('request')
pscifinq = ElementTree.SubElement(root, 'pscifinq')
bank = ElementTree.SubElement(pscifinq, 'bank')
bank.text = '1'
inquiryString = ElementTree.tostring(root)
In the most recent version, ElementTree modules are part of the
elementtree package. Are you using an older version? If so, perhaps
you should get the latest version.
4) the term 'ElementTree files' referenced above refers to the
following files:
__init__.py (this file contains only comments)
ElementInclude.py
ElementPath.py
ElementTree.py
HTMLTreeBuilder.py
SgmlopXMLTreeBuilder.py
SimpleXMLTreeBuilder.py
SimpleXMLWriter.py
TidyHTMLTreeBuilder.py
TidyTools.py
XMLTreeBuilder.py
It looks like your version of ElementTree is a packaged version. The
file __init__.py normally appears only in packages; it's a mistake for
these files to have been in the workarea directory in the first place.
How did you install ElementTree?
Want to change things as follows:

Folder structure:

\workarea\ <- ElementTree files no longer here
\xml\
\dom\
\elementtree\ <- ElementTree files reside here
\parsers\
\sax\
Bad idea, I'd say. Generally, you shouldn't inject your own modules
into someone else's package system (unless you're working on someone
else's packages, to modify or enhance them). The xml package might
have been a slight exception to this, seeing how it's just a container
for several related packages, but it's in the Python distribution.

ElementTree modules are part of the elementtree package. You should
arrange your directories like this (and they should have been arranged
like this in the first place):

\workarea
\elementtree
\xml
\dom
\sax
...etc...

I tried changing the

import ElementTree

statement to:

import xml.elementtree.ElementTree

The result of changing the folder structure and the import statement
was the following error:

import xml.elementtree.ElementTree

ImportError: No module named elementtree.ElementTree
I'm guessing this exception is not happening with your import
statement, but with some other import in one of the ElementTree modules
(though in that case I'm not sure why it would have been working
before). I'd have to see the traceback to be sure. Generally, when
reporting an error, you should include a traceback.

I verified that the file ElementTree.py really does reside in the
\workarea\xml\elementtree\ folder. Assuming that I really want the
ElementTree files to reside in the \workarea\xml\elementtree\ folder,
what changes must I make such that the script can locate the
ElementTree.py file? I have a hunch that there is something obvious
that I am missing in the import statement; is it possible to accomplish
this by changing only the import statement rather than changing each of
the calls to the Element(), SubElement(), XML() and tostring() methods.


Well, if you arrange it as I advise, you shouldn't have a problem.
However, if you want to change only the import statements, you don't
want to do this:

import elementtree.ElementTree

That will import ElementTree but the you'd have to access it as
elementtree.ElementTree. Instead you should do this:

from elementtree import ElementTree
Carl Banks

Nov 5 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.