PyWart: Import resolution order

Rick Johnson rantingrickjohnson at gmail.com
Sat Jan 12 05:50:34 CET 2013


On Friday, January 11, 2013 7:35:37 AM UTC-6, Terry Reedy wrote:
> On 1/11/2013 1:13 AM, Rick Johnson wrote:
> > The fact that Python looks in the stdlib _first_ is not a good idea.
> 
> And the fact is that it does not do so. The order depends on sys.path, 
> and '' is the first entry.
> 
> > It would seem more intuitive for a custom "math" module (living in
> > the current directory) to /override/ the stlib "math" module.
>
> This is a nuisance though, when not intended. Someone writes a 
> random.py, and a year later in the same directory, an unrelated 
> hopscotch.py, which tries to import random.exponential. The import fails 
> and they post here, having forgotten about their own random.py, which 
> does not have such a function. Posts like this happen a few times a year.

That's why i also mentioned the failure of Python to wrap stdlib modules in a package. If we would protect all built-in modules by placing them in a package (lib or py) then this problem would never happen. 

Of course many people will piss and moan about the extra typing. I say, you have a choice: a few extra chars or multitudes of extra headaches -- I choose the first option.

Since more time is spent /maintaining/ code bases than /writing/ them, the explicit path is always the correct path to choose. Anyone who says otherwise is either careless or selfish (aka: seeking job security).



More information about the Python-list mailing list