Skip to content

ERR: include original error message for missing required dependencies #26685

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 14 commits into from
Jun 11, 2019
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion doc/source/whatsnew/v0.25.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ Other Enhancements
- :meth:`DataFrame.query` and :meth:`DataFrame.eval` now supports quoting column names with backticks to refer to names with spaces (:issue:`6508`)
- :func:`merge_asof` now gives a more clear error message when merge keys are categoricals that are not equal (:issue:`26136`)
- :meth:`pandas.core.window.Rolling` supports exponential (or Poisson) window type (:issue:`21303`)
-
- Error message for missing required imports now includes the original ImportError's text (:issue:`23868`)

.. _whatsnew_0250.api_breaking:

Expand Down
12 changes: 8 additions & 4 deletions pandas/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,16 @@
try:
__import__(dependency)
except ImportError as e:
missing_dependencies.append(dependency)
missing_dependencies.append((dependency, e))

msg = None
ex = None
if missing_dependencies:
raise ImportError(
"Missing required dependencies {0}".format(missing_dependencies))
del hard_dependencies, dependency, missing_dependencies
msg = "Unable to import required dependencies:"
for dependency, ex in missing_dependencies:
msg += "\n{0}: {1}".format(dependency, str(ex))
raise ImportError(msg)
del hard_dependencies, dependency, missing_dependencies, ex, msg

# numpy compat
from pandas.compat.numpy import (
Expand Down
31 changes: 31 additions & 0 deletions pandas/tests/test_base.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import builtins
from datetime import datetime, timedelta
from importlib import reload
from io import StringIO
import re
import sys
Expand Down Expand Up @@ -1341,3 +1343,32 @@ def test_to_numpy_dtype(as_series):
expected = np.array(['2000-01-01T05', '2001-01-01T05'],
dtype='M8[ns]')
tm.assert_numpy_array_equal(result, expected)


def test_missing_required_dependency(monkeypatch):
original_import = __import__

def mock_import_fail(name, *args, **kwargs):
if name == "numpy":
raise ImportError("cannot import name numpy")
elif name == "pytz":
raise ImportError("cannot import name some_dependency")
elif name == "dateutil":
raise ImportError("cannot import name some_other_dependency")
else:
return original_import(name, *args, **kwargs)

expected_msg = (
"Unable to import required dependencies:"
"\nnumpy: cannot import name numpy"
"\npytz: cannot import name some_dependency"
"\ndateutil: cannot import name some_other_dependency"
)

with monkeypatch.context() as m:
m.setattr(builtins, "__import__", mock_import_fail)
with pytest.raises(ImportError, match=expected_msg):
reload(pd)

# Final reload to ensure normal state of pandas
reload(pd)