Skip to content

Shouldn't the try: __import__ in __init__ re-raise original exception? #23868

@haphaeu

Description

@haphaeu

Problem description

It is confusing when import pandas raises a message like:

>>> import pandas
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/raf/anaconda3/lib/python3.7/site-packages/pandas/__init__.py", line 19, in <module>
    "Missing required dependencies {0}".format(missing_dependencies))
ImportError: Missing required dependencies ['pytz']

Where actually the true error is not in pytz, but in a missing library required by pytz:

>>> import pytz
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/raf/anaconda3/lib/python3.7/site-packages/pytz/__init__.py", line 20, in <module>
    from pytz.tzinfo import unpickler, BaseTzInfo
  File "/home/raf/anaconda3/lib/python3.7/site-packages/pytz/tzinfo.py", line 4, in <module>
    from bisect import bisect_right
ImportError: cannot import name 'bisect_right' from 'bisect' (/home/raf/tmp/bisect.py)

As you can see, I happen to have a file called bisect.py in the same folder where I'm trying to import pandas, but the error message raised by pandas doesn't reflect that. It was a silly error after all, but It gave me a headache to sort that out.

Tracking back the source of it, in pandas/__init__.py, note that the original error e is not used after except ImportError as e. I think it should.

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

if missing_dependencies:
    raise ImportError(
        "Missing required dependencies {0}".format(missing_dependencies))

Expected Output

raise ImportError(...) should mention the error e raised by __import__(dependency)


Question also asked in SO:
https://stackoverflow.com/questions/53445555/shouldnt-the-try-import-in-init-re-raise-original-exception

Metadata

Metadata

Assignees

No one assigned

    Labels

    Error ReportingIncorrect or improved errors from pandasNeeds DiscussionRequires discussion from core team before further actiongood first issue

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions