Skip to content

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

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 2 commits into from
Jun 5, 2019
Merged
Show file tree
Hide file tree
Changes from all 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
8 changes: 5 additions & 3 deletions pandas/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,13 @@
try:
__import__(dependency)
except ImportError as e:
missing_dependencies.append(dependency)
missing_dependencies.append((dependency, e))

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

# numpy compat
Expand Down
27 changes: 27 additions & 0 deletions pandas/tests/test_base.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
from datetime import datetime, timedelta
from importlib import reload
from io import StringIO
import re
import sys
from unittest.mock import patch

import numpy as np
import pytest
Expand Down Expand Up @@ -1341,3 +1343,28 @@ 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)


@patch("builtins.__import__")
def test_missing_required_dependency(mock_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 __import__(name, *args, **kwargs)

mock_import.side_effect = mock_import_fail

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 pytest.raises(ImportError, match=expected_msg):
reload(pd)