python xml.sax找不到解析器py2exe

时间:2015-05-13 09:12:09

标签: python xml

我试图让我的游戏运行起来。代码需要xml.sax。我用py2exe导入它,它仍然不想工作。我已经尝试了好几天而且我无法上班。在将它编译为.exe之前,它可以工作,但每当我将它编译为exe时,它都无法工作。

运行:

[ - ] Python 2.7.9 32位

[ - ] py2exe 0.6.9 32位

错误是:SAXReaderNotAvailable: No parsers found

这是我的游戏代码:

import xml.sax

class DNAError(Exception): pass
class DNAParseError(DNAError): pass

elementRegistry = {}
def registerElement(element):
  elementRegistry[element.TAG] = element

class DNASaxHandler(xml.sax.ContentHandler):
def __init__(self):
    xml.sax.ContentHandler.__init__(self)

    self.stack = []
    self.root = None

def startElement(self, tag, attrs):
    if self.stack:
        parent = self.stack[-1]
        parentTag = parent.TAG
    else:
        parent = None
        parentTag = None

    element = elementRegistry.get(tag)
    if not element:
        raise DNAParseError('Unknown element type: ' + tag)

    if parentTag not in element.PARENTS:
        raise DNAParseError('Cannot put %s below %s element' % (tag, parentTag))

    element = element(**attrs)
    self.stack.append(element)
    element.reparentTo(parent)

    if not self.root:
        self.root = element

def endElement(self, tag):
    self.stack.pop(-1)

def characters(self, chars):
    if not self.stack:
        return

    self.stack[-1].handleText(chars)

def parse(stream):
handler = DNASaxHandler()
xml.sax.parse(stream, handler)
return handler.root

这是py2exe的编译脚本:

from distutils.core import setup
import py2exe

setup(
options={"py2exe": {"includes": [ "xml.sax.drivers.*", "xml.sax.drivers2.*", "xml.sax" ] }},
zipfile = None,
windows = ['relived.py']
)

1 个答案:

答案 0 :(得分:0)

尝试将此添加到setup.py py2exe命令行:

--packages xml.sax.drivers,xml.sax.drivers2

Mark Byer's answer