PyWart: Import resolution order

Ian Kelly ian.g.kelly at gmail.com
Sat Jan 12 08:36:43 CET 2013


On Fri, Jan 11, 2013 at 10:28 PM, Rick Johnson
<rantingrickjohnson at gmail.com> wrote:
> On Friday, January 11, 2013 12:30:27 AM UTC-6, Chris Angelico wrote:
>> Why is it better to import from the current directory first?
>
> Opps. I was not explicit enough with my explanation :). I meant, "look in the current directory FIRST when in a package". Since many times (most all times) packages will contain many sub-modules that need to be imported into the package's main.py module, and sometimes these modules will have the same name as a stdlib module, then looking in the package FIRST makes sense.

And again, in Python 2.x this is already the case.  When importing in
a package, it tries to do a relative import before it even looks at
sys.path.

> I think if python where *strict* about full paths for non-builtins, then we would be in a better place.

And again, in Python 3, where implicit relative imports have been
removed from the language, it already is strict about using full
paths.  You can still do relative imports, but you have to be explicit
about them.

> For instance you could create a package named "chris" and then have a module named math exist inside. Alternatively if you choose to be a non-professional and create a math module without a containing package, python would throw the module into the default "lib" package. The only way you could access your math module now would be by using the path "lib.math".

What if I create a package named "math"?  Does that also automatically
get renamed to "lib.math"?  How is it decided what package names are
proper; is it just because it happens to clash with a stdlib name that
the package gets magically renamed?

What if I create a package, and then later a module with the same name
happens to be added to the stdlib?  My program that uses the package
just breaks because it no longer imports the correct thing?

> Damn i am full of good ideas!

Your ideas might be better if you first spent some time gaining a
better understanding of how the language works as is.



More information about the Python-list mailing list