Python 2.7-没有名为expat的模块;改用SimpleXMLTreeBuilder-源码安装

时间:2018-10-25 15:12:53

标签: python openpyxl elementtree

我在不允许安装Python的服务器上工作(pip,easy_install),因此我必须将源代码放到本地目录中才能运行Python + openpyxl。

我有可用的Python2.7和openpyxl-2.5.8,以及openpyxl依赖项。但是,当openpyxl调用builtins.py时,我得到了

ImportError: No module named expat; use SimpleXMLTreeBuilder instead

expat.py在python2.7 / Lib / xml / parsers中可用,但我认为由于“安装” Python的方式,它无法识别某些内容,可能与C扩展有关(基于{{3 }})。

https://groups.google.com/forum/#!topic/google-appengine/tzmakclSXJc,按照@thom_nic的建议,我获得了elementtree的旧源代码,并尝试将ElementTree中的XMLTreeBuilder指向SimpleXMLTreeBuilder:

from xml.etree import ElementTree
from elementtree import SimpleXMLTreeBuilder 
ElementTree.XMLTreeBuilder = SimpleXMLTreeBuilder.TreeBuilder 

但是,我仍然遇到相同的错误。以下是完整的追溯,以供参考。关于如何解决此问题的任何想法,因为我无法重新安装Python或运行yum update或类似的东西?

跟踪:

Traceback (most recent call last):
  File "/home/python_packages/python2.7/Lib/runpy.py", line 174, in _run_module_as_main
    "__main__", fname, loader, pkg_name)
  File "/home/python_packages/python2.7/Lib/runpy.py", line 72, in _run_code
    exec code in run_globals
  File "/home/release/create_sheet.py", line 18, in <module>
    from openpyxl.workbook import Workbook
  File "/home/python_packages/openpyxl-2.5.8/openpyxl/__init__.py", line 6, in <module>
    from openpyxl.workbook import Workbook
  File "/home/python_packages/openpyxl-2.5.8/openpyxl/workbook/__init__.py", line 5, in <module>
    from .workbook import Workbook
  File "/home/python_packages/openpyxl-2.5.8/openpyxl/workbook/workbook.py", line 7, in <module>
    from openpyxl.worksheet import Worksheet
  File "/home/python_packages/openpyxl-2.5.8/openpyxl/worksheet/__init__.py", line 4, in <module>
    from .worksheet import Worksheet
  File "/home/python_packages/openpyxl-2.5.8/openpyxl/worksheet/worksheet.py", line 35, in <module>
    from openpyxl.cell import Cell
  File "/home/python_packages/openpyxl-2.5.8/openpyxl/cell/__init__.py", line 4, in <module>
    from .cell import Cell, WriteOnlyCell
  File "/home/python_packages/openpyxl-2.5.8/openpyxl/cell/cell.py", line 46, in <module>
    from openpyxl.styles.styleable import StyleableObject
  File "/home/python_packages/openpyxl-2.5.8/openpyxl/styles/styleable.py", line 11, in <module>
    from .builtins import styles
  File "/home/python_packages/openpyxl-2.5.8/openpyxl/styles/builtins.py", line 1350, in <module>
    ('Normal', NamedStyle.from_tree(fromstring(normal))),
  File "/home/python_packages/python2.7/Lib/xml/etree/ElementTree.py", line 1310, in XML
    parser = XMLParser(target=TreeBuilder())
  File "/home/python_packages/python2.7/Lib/xml/etree/ElementTree.py", line 1482, in __init__
    "No module named expat; use SimpleXMLTreeBuilder instead"
ImportError: No module named expat; use SimpleXMLTreeBuilder instead

1 个答案:

答案 0 :(得分:1)

看起来像是严重令人厌烦的Python 2.7安装。 SimpleXMLBuilder提示几乎可以肯定是不相关的。如果您可以使用pip在本地安装lxml,则可以在其中进行补丁,但我怀疑您的依赖项会遇到更多问题。但是基本上,您需要一个干净且更新的Python 2.7。