Any ideas on this confusing Python "No module named expat; Use SimpleXMLTreeBuilderInstead" error?

4.9k views Asked by At

Here's the scenario - I know this question has already been asked before on StackOverflow in slightly different situations, but unfortunately none of the answers/suggestions provided have been of any help to me.

I have a Python file that's being passed as an argument to a Python API for another program (PSS\E) to use on startup. Inside my Python file, I have a section where I'm using the ElementTree library to parse an XML file into a tree structure. Here is the code I have:

from xml.etree.ElementTree import ElementTree, Element
...
tree = ElementTree.parse(myXmlFileName)

Whenever I make the call to the ElementTree.parse(myXmlFileName) method, I get the following error:

Import Error No module named expat; use SimpleXMLTreeBuilder instead

The bizzare thing is that this error is being thrown from inside the ElementTree.py file itself, inside of the XMLParser class definition:

class XMLParser(object):
    def __init__(self, html=0, target=None, encoding=None):
        try:
            from xml.parsers import expat
        except ImportError:
            raise ImportError("No module named expat; use SimpleXMLTreeBuilder instead")

I've tried using SimpleXMLTreeBuilder instead, per some of the other answers to this question on StackOverflow, but it gives me the exact same error.

If I open my Python shell (I'm using Python 2.7.8, by the way) and type "import xml.parsers.expat", I don't receive this error and everything appears to be imported normally. I can also run the ElementTree.parse(myXmlFilePath) command without any issues inside the Python shell. However, when I try to do this via my Python file that I'm feeding into the PSS\E API (PSSE is a third-party product with a Python API), I get the error mentioned above by following the same steps that are successful in the Python shell.

Do any Python gurus have any advice on this one? I wouldn't think the file path would make a difference since my system's path variable includes the location of my Python installation, and I've tried using this code in the Python shell from different "home" directories and it seems to work no matter what I try. The problem only happens when I try to interface to this third party application.

Thanks for any help!

1

There are 1 answers

0
Codine On BEST ANSWER

Thanks for the suggestion VooDooNOFX - after some discussion with Siemens Support, it was revealed that my command line execution of psse33.exe needed an additional argument "-pyver 27" to specify which version of Python the application needed to use. Hopefully this helps anyone else running into this issue.