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 2 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
10 changes: 5 additions & 5 deletions pandas/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,21 @@
# Let users know if they're missing any of our hard dependencies
hard_dependencies = ("numpy", "pytz", "dateutil")
missing_dependencies = []
msg = None
e = None

for dependency in hard_dependencies:
try:
__import__(dependency)
except ImportError as e:
missing_dependencies.append((dependency, e))

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

# numpy compat
from pandas.compat.numpy import (
Expand Down
8 changes: 4 additions & 4 deletions pandas/tests/test_base.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import builtins
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 @@ -1345,7 +1345,7 @@ def test_to_numpy_dtype(as_series):
tm.assert_numpy_array_equal(result, expected)


def test_missing_required_dependency():
def test_missing_required_dependency(monkeypatch):
original_import = __import__

def mock_import_fail(name, *args, **kwargs):
Expand All @@ -1365,8 +1365,8 @@ def mock_import_fail(name, *args, **kwargs):
"\ndateutil: cannot import name some_other_dependency"
)

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

Expand Down