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 13 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 import error's text (:issue:`23868`)

.. _whatsnew_0250.api_breaking:

Expand Down
5 changes: 2 additions & 3 deletions pandas/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,10 @@
try:
__import__(dependency)
except ImportError as e:
missing_dependencies.append(dependency)
missing_dependencies.append("{0}: {1}".format(dependency, str(e)))

if missing_dependencies:
raise ImportError(
"Missing required dependencies {0}".format(missing_dependencies))
raise ImportError("Unable to import required dependencies:\n" + "\n".join(missing_dependencies))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you do this like

raise ImportError("Unable to import required dependencies:\n" 
                              "\n".join(missing_dependencies))

since inside the parens you don't need the +

Copy link
Contributor Author

@DanielFEvans DanielFEvans Jun 10, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That gives a different output - the current one joins the dependencies with \n:

Unable to import required dependencies:
a
b
c

Whereas without the "+" it joins them with the entire error message and two newlines:

aUnable to import required dependencies:

bUnable to import required dependencies:

c

del hard_dependencies, dependency, missing_dependencies

# numpy compat
Expand Down
30 changes: 30 additions & 0 deletions pandas/tests/test_downstream.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""
Testing that we work in the downstream packages
"""
import builtins
import importlib
import subprocess
import sys
Expand Down Expand Up @@ -131,3 +132,32 @@ def test_pyarrow(df):
table = pyarrow.Table.from_pandas(df)
result = table.to_pandas()
tm.assert_frame_equal(result, df)


def test_missing_required_dependency(monkeypatch):
# GH 23868
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"
)

import pandas as pd

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