Skip to content
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

BUG: Improve ImportError message to suggest importing dependencies directly for full error details #61084

Open
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

chilin0525
Copy link
Contributor

@chilin0525
Copy link
Contributor Author

Hi @rhshadrach , I have implemented your suggestion from the #61030. Could you please verify if this implementation meets your expectations? Thank you!

Below are example outputs before and after this change:

  • before:
    >>> import pandas as pd
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      File "/home/pandas/pandas/__init__.py", line 16, in <module>
        raise ImportError(
    ImportError: Unable to import required dependencies:
    numpy: Error importing numpy: you should not try to import numpy from
            its source directory; please exit the numpy source tree, and relaunch
            your python interpreter from there.
  • after change:
    >>> import pandas as pd
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      File "/home/pandas/pandas/__init__.py", line 16, in <module>
        raise ImportError(
    ImportError: Unable to import required dependencies:
    numpy: Error importing numpy: you should not try to import numpy from
            its source directory; please exit the numpy source tree, and relaunch
            your python interpreter from there.
    
    To see the full error message, try importing the missing dependencies directly.

Copy link
Member

@rhshadrach rhshadrach left a comment

Choose a reason for hiding this comment

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

@mroeschke - what do you think about adding tzdata to the _hard_dependencies on L6 here.

"Unable to import required dependencies:\n" + "\n".join(_missing_dependencies)
"Unable to import required dependencies:\n"
+ "\n".join(_missing_dependencies)
+ "\n\nTo see the full error message, "
Copy link
Member

Choose a reason for hiding this comment

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

nit: You can remove the + on each line; Python will perform string concatenation automatically.

Also, I think we should remove one of the \n here:

ImportError: Unable to import required dependencies:
dateutil: No module named 'dateutil'

To see the full error message, try importing the missing dependencies directly.

vs

ImportError: Unable to import required dependencies:
dateutil: No module named 'dateutil'
To see the full error message, try importing the missing dependencies directly.

By removing the blank line, it is more apparent to me that it's all part of the same error message.

Copy link
Member

Choose a reason for hiding this comment

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

Hmm what if we just change this to

for _dependency in _hard_dependencies:
    try:
        __import__(_dependency)
    except ImportError as _e:  # pragma: no cover
       raise ImportError(f"Unable to import required dependency {_dependency} ...") from _e

I know we won't be able to collect all the missing dependencies all at once but at least we'll maintain the traceback of the failed import

Copy link
Member

@rhshadrach rhshadrach Mar 10, 2025

Choose a reason for hiding this comment

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

@mroeschke - I'm good with your suggested approach, especially given how few hard dependencies pandas has.

@rhshadrach rhshadrach added Build Library building on various platforms Error Reporting Incorrect or improved errors from pandas labels Mar 9, 2025
@rhshadrach rhshadrach added this to the 3.0 milestone Mar 9, 2025
@mroeschke
Copy link
Member

what do you think about adding tzdata to the _hard_dependencies on L6 here.

Yeah we should probably add that too.

@chilin0525 chilin0525 marked this pull request as draft March 11, 2025 08:11
@chilin0525
Copy link
Contributor Author

what do you think about adding tzdata to the _hard_dependencies on L6 here.

Yeah we should probably add that too.

@mroeschke For tzdata, should I add the changes in this PR or create a separate issue? Thanks!

@chilin0525 chilin0525 marked this pull request as ready for review March 11, 2025 15:04
@mroeschke
Copy link
Member

For tzdata, should I add the changes in this PR or create a separate issue?

Please create a separate issue for this

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Build Library building on various platforms Error Reporting Incorrect or improved errors from pandas
Projects
None yet
Development

Successfully merging this pull request may close these issues.

BUG: Dependency check custom error loses information
3 participants