From c2759cb05933711508bebeeda01d3dd28d0a0da9 Mon Sep 17 00:00:00 2001 From: Guilherme Beltramini Date: Wed, 20 Sep 2017 19:22:16 -0300 Subject: [PATCH 1/2] Throw ImportError --- doc/source/whatsnew/v0.21.0.txt | 1 + pandas/io/excel.py | 16 ++++++++++------ 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/doc/source/whatsnew/v0.21.0.txt b/doc/source/whatsnew/v0.21.0.txt index 42174c228ef3c..9cc3d8b998584 100644 --- a/doc/source/whatsnew/v0.21.0.txt +++ b/doc/source/whatsnew/v0.21.0.txt @@ -115,6 +115,7 @@ Other Enhancements - :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`) - :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`). - :func:`MultiIndex.is_monotonic_decreasing` has been implemented. Previously returned ``False`` in all cases. (:issue:`16554`) +- :func:`read_excel` raises ``ImportError`` with better message if ``xlrd`` is not installed .. _whatsnew_0210.api_breaking: diff --git a/pandas/io/excel.py b/pandas/io/excel.py index 5db4603c37be0..f11fc20975bd0 100644 --- a/pandas/io/excel.py +++ b/pandas/io/excel.py @@ -239,12 +239,16 @@ class ExcelFile(object): def __init__(self, io, **kwds): - import xlrd # throw an ImportError if we need to - - ver = tuple(map(int, xlrd.__VERSION__.split(".")[:2])) - if ver < (0, 9): # pragma: no cover - raise ImportError("pandas requires xlrd >= 0.9.0 for excel " - "support, current version " + xlrd.__VERSION__) + err_msg = "Install xlrd >= 0.9.0 for Excel support" + try: + import xlrd + except ImportError: + raise ImportError(err_msg) + else: + ver = tuple(map(int, xlrd.__VERSION__.split(".")[:2])) + if ver < (0, 9): # pragma: no cover + raise ImportError(err_msg + + ". Current version " + xlrd.__VERSION__) # could be a str, ExcelFile, Book, etc. self.io = io From dee199820deb19b67ca333ebb81a4cf7eb80f610 Mon Sep 17 00:00:00 2001 From: Guilherme Beltramini Date: Thu, 21 Sep 2017 17:03:39 -0300 Subject: [PATCH 2/2] Add PR number and blank line --- doc/source/whatsnew/v0.21.0.txt | 2 +- pandas/io/excel.py | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v0.21.0.txt b/doc/source/whatsnew/v0.21.0.txt index 9cc3d8b998584..1b70c2d6104d7 100644 --- a/doc/source/whatsnew/v0.21.0.txt +++ b/doc/source/whatsnew/v0.21.0.txt @@ -115,7 +115,7 @@ Other Enhancements - :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`) - :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`). - :func:`MultiIndex.is_monotonic_decreasing` has been implemented. Previously returned ``False`` in all cases. (:issue:`16554`) -- :func:`read_excel` raises ``ImportError`` with better message if ``xlrd`` is not installed +- :func:`read_excel` raises ``ImportError`` with better message if ``xlrd`` is not installed. (:issue:`17613`) .. _whatsnew_0210.api_breaking: diff --git a/pandas/io/excel.py b/pandas/io/excel.py index f11fc20975bd0..faafdba435ff2 100644 --- a/pandas/io/excel.py +++ b/pandas/io/excel.py @@ -240,6 +240,7 @@ class ExcelFile(object): def __init__(self, io, **kwds): err_msg = "Install xlrd >= 0.9.0 for Excel support" + try: import xlrd except ImportError: