Importing load_workbook fails with Python 2.7.5

Issue #223 resolved
Anonymous created an issue

When importing load_workbook (from openpyxl.reader.excel import load_workbook) with Python 2.7.5, the import fails because xml.sax.saxutils does not have global name _error_handling anymore. It worked still in 2.7.3, didn't try 2.7.4 but looking at the 2.7.4 release notes there seems to be some updates to saxutils already.

Traceback (most recent call last):
  File "C:\generate_ff_tests_from_excel.py", line 255, in <module>
    excel_contents = Read_data_from_Excel(excel_filename)
  File "C:\generate_ff_tests_from_excel.py", line 14, in Read_data_from_Excel
    from openpyxl.reader.excel import load_workbook
  File "build\bdist.win32\egg\openpyxl\__init__.py", line 29, in <module>
  File "build\bdist.win32\egg\openpyxl\cell.py", line 37, in <module>
  File "build\bdist.win32\egg\openpyxl\shared\__init__.py", line 33, in <module>
  File "build\bdist.win32\egg\openpyxl\shared\xmltools.py", line 35, in <module>
  File "C:\Python27\lib\site-packages\openpyxl-1.6.2-py2.7.egg\openpyxl\shared\compat\sax.py", line 56, in <module>
ImportError: cannot import name _error_handling

Comments (10)

  1. Eric Gazoni

    Indeed, but there are still some compatibility issues to be ironed out, it seems that the last batch of changes I made broke compatibility with virtually every version of Python except 2.7.3 :-D

    Thanks for reporting, as you can see here, the CI build passes on 2.7, but I didn't know I also had to be defensive regarding minor python version numbers, thanks for pointing it out

  2. CharlieC

    FWIW there have been an awful lot of breakages in the 2.7 series due to maintainers not backporting stuff from Python 3 very carefully. A bug should probably be submitted for this, though of course it's arguable that the API can be broken as _error_handling can be considered to be private.

  3. Eric Gazoni

    well, in the case of _error_handling, I plead guilty though :D I looked up in the docs to see if it was available, but I did miss something apparenlty. I'll fix it up asap.

  4. Dirk Makowski

    From a fresh clone I ran into the same issue. I (dirtily) fixed it like this in shared / compat / sax.py:

    else:
        CompatXMLGenerator = _XMLGenerator
        try:
            from xml.sax.saxutils import _error_handling
        except ImportError:
            _error_handling = "xmlcharrefreplace"
    
  5. CharlieC

    @Dirk Makowski That isn't actually the solution. That will only mask the import error. The real problem is exposed if you run the tests because the _write(self) is always overwritten in the SAX library when the class is instantiated:

    `
    class XMLGenerator(handler.ContentHandler):

    def __init__(self, out=None, encoding="iso-8859-1"):
        handler.ContentHandler.__init__(self)
        out = _gettextwriter(out, encoding)
        self._write = out.write
    

    `

    This leads to encoding errors when ._write() is called. The errors disappear when the class is removed.

  6. Alex Garnett

    Has this fix made it upstream yet? The version in pip was still broken on 2.7.4 (current Python version in Ubuntu) last I checked... I wasn't having much luck trying to manually replicate the fixes suggested in this thread.

  7. Log in to comment