On Thu, Nov 4, 2021 at 6:37 AM Ethan Furman <email@example.com> wrote:
> On 11/3/21 12:21 PM, Florian Wetschoreck wrote:
> > In order to prevent confusion, I want to point out that the primary scenario that I meant is not that the file imports
> > itself but another file in the same directory with the name of a module that is also installed in site-packages. Not
> > sure if I made that clear in my original post?
> Maybe if the module-level AttributeError included the complete path/filename of the module? It would then be much more
> easily discernible that the imported "pandas" is not the correct one.
I like this idea. It already happens with a from-import. A cursory
look at Objects/moduleobject.c suggests that it'd be one more lookup
in the error handling in module_getattro, pretty doable.
Why not go one step further and let the user know there is/was another pandas waiting in line to be imported.... "maybe you intended to import module of the same name at these other locations".
This would be an expensive disk read if you did it at the time the exception was raised, but what if at import time python kept a reference around to the other candidate locations of a module name that would have been imported? Most of the time those locations would not have changed.
And furthermore to reduce any hit to performance you could have it only site this information for this helpful error message if the import came from the local directory and not somewhere like site packages. Though I don't know how people would feel about different error messages bubbling up depending on where a module was imported from....