Skip to content

Commit c2759cb

Browse files
author
Guilherme Beltramini
committed
Throw ImportError
1 parent fedf922 commit c2759cb

File tree

2 files changed

+11
-6
lines changed

2 files changed

+11
-6
lines changed

doc/source/whatsnew/v0.21.0.txt

+1
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ Other Enhancements
115115
- :func:`DataFrame.items` and :func:`Series.items` is now present in both Python 2 and 3 and is lazy in all cases (:issue:`13918`, :issue:`17213`)
116116
- :func:`Styler.where` has been implemented. It is as a convenience for :func:`Styler.applymap` and enables simple DataFrame styling on the Jupyter notebook (:issue:`17474`).
117117
- :func:`MultiIndex.is_monotonic_decreasing` has been implemented. Previously returned ``False`` in all cases. (:issue:`16554`)
118+
- :func:`read_excel` raises ``ImportError`` with better message if ``xlrd`` is not installed
118119

119120

120121
.. _whatsnew_0210.api_breaking:

pandas/io/excel.py

+10-6
Original file line numberDiff line numberDiff line change
@@ -239,12 +239,16 @@ class ExcelFile(object):
239239

240240
def __init__(self, io, **kwds):
241241

242-
import xlrd # throw an ImportError if we need to
243-
244-
ver = tuple(map(int, xlrd.__VERSION__.split(".")[:2]))
245-
if ver < (0, 9): # pragma: no cover
246-
raise ImportError("pandas requires xlrd >= 0.9.0 for excel "
247-
"support, current version " + xlrd.__VERSION__)
242+
err_msg = "Install xlrd >= 0.9.0 for Excel support"
243+
try:
244+
import xlrd
245+
except ImportError:
246+
raise ImportError(err_msg)
247+
else:
248+
ver = tuple(map(int, xlrd.__VERSION__.split(".")[:2]))
249+
if ver < (0, 9): # pragma: no cover
250+
raise ImportError(err_msg +
251+
". Current version " + xlrd.__VERSION__)
248252

249253
# could be a str, ExcelFile, Book, etc.
250254
self.io = io

0 commit comments

Comments
 (0)