Skip to content

Commit ea06f8d

Browse files
DanielFEvansjorisvandenbossche
authored andcommitted
ERR: include original error message for missing required dependencies (#26685)
1 parent 157a4e3 commit ea06f8d

File tree

3 files changed

+34
-4
lines changed

3 files changed

+34
-4
lines changed

doc/source/whatsnew/v0.25.0.rst

+2-1
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,8 @@ Other Enhancements
9696
- :meth:`DataFrame.query` and :meth:`DataFrame.eval` now supports quoting column names with backticks to refer to names with spaces (:issue:`6508`)
9797
- :func:`merge_asof` now gives a more clear error message when merge keys are categoricals that are not equal (:issue:`26136`)
9898
- :meth:`pandas.core.window.Rolling` supports exponential (or Poisson) window type (:issue:`21303`)
99-
- :class:`DatetimeIndex` and :class:`TimedeltaIndex` now have a `mean` method (:issue:`24757`)
99+
- Error message for missing required imports now includes the original import error's text (:issue:`23868`)
100+
- :class:`DatetimeIndex` and :class:`TimedeltaIndex` now have a ``mean`` method (:issue:`24757`)
100101
- :meth:`DataFrame.describe` now formats integer percentiles without decimal point (:issue:`26660`)
101102

102103
.. _whatsnew_0250.api_breaking:

pandas/__init__.py

+2-3
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,10 @@
1010
try:
1111
__import__(dependency)
1212
except ImportError as e:
13-
missing_dependencies.append(dependency)
13+
missing_dependencies.append("{0}: {1}".format(dependency, str(e)))
1414

1515
if missing_dependencies:
16-
raise ImportError(
17-
"Missing required dependencies {0}".format(missing_dependencies))
16+
raise ImportError("Unable to import required dependencies:\n" + "\n".join(missing_dependencies))
1817
del hard_dependencies, dependency, missing_dependencies
1918

2019
# numpy compat

pandas/tests/test_downstream.py

+30
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
"""
22
Testing that we work in the downstream packages
33
"""
4+
import builtins
45
import importlib
56
import subprocess
67
import sys
@@ -131,3 +132,32 @@ def test_pyarrow(df):
131132
table = pyarrow.Table.from_pandas(df)
132133
result = table.to_pandas()
133134
tm.assert_frame_equal(result, df)
135+
136+
137+
def test_missing_required_dependency(monkeypatch):
138+
# GH 23868
139+
original_import = __import__
140+
141+
def mock_import_fail(name, *args, **kwargs):
142+
if name == "numpy":
143+
raise ImportError("cannot import name numpy")
144+
elif name == "pytz":
145+
raise ImportError("cannot import name some_dependency")
146+
elif name == "dateutil":
147+
raise ImportError("cannot import name some_other_dependency")
148+
else:
149+
return original_import(name, *args, **kwargs)
150+
151+
expected_msg = (
152+
"Unable to import required dependencies:"
153+
"\nnumpy: cannot import name numpy"
154+
"\npytz: cannot import name some_dependency"
155+
"\ndateutil: cannot import name some_other_dependency"
156+
)
157+
158+
import pandas as pd
159+
160+
with monkeypatch.context() as m:
161+
m.setattr(builtins, "__import__", mock_import_fail)
162+
with pytest.raises(ImportError, match=expected_msg):
163+
importlib.reload(pd)

0 commit comments

Comments
 (0)