SyntaxError: 'import *' not allowed with 'from .'

George Sakkis george.sakkis at gmail.com
Mon Jan 14 18:09:08 EST 2008


On Jan 14, 6:01 pm, Ben Finney <bignose+hates-s... at benfinney.id.au>
wrote:

> George Sakkis <george.sak... at gmail.com> writes:
> > Unless I missed it, PEP 328 doesn't mention anything about this.
> > What's the reason for not allowing "from .relative.module import *'
> > ?
>
> It makes the code much harder to follow visually and inspect with
> static analysis tools, since there's no way to see where names come
> from in the code. It defeats the purpose of separate namespaces,
> confusing the imported module's names with the current module's names
> in a way that makes the indistinguishable.
>
> If you want to use all or most of the names in a module, keep them in
> their own namespace:
>
>     import spam
>     import eggs
>
>     spam.do_stuff()
>     eggs.do_stuff()
>
> If you don't like the name of the module, then use whatever one suits
> you:
>
>     import your_mother_was_a_hamster as spam
>     import your_father_smelled_of_elderberries as eggs
>
>     spam.do_stuff()
>     eggs.do_stuff()
>
> Both of these are superior to 'from spam import *' because it's clear
> (to the reader and to static analysis tools) where every name comes
> from: unqualified names must be defined in the current module, any
> ones from the imported module are qualified with the module name.
>
> You also, in cases like the above example, avoid unknowingly
> clobbering existing names by importing from another module into the
> current namespace.

All the above are well-known and apply to both absolute and relative
imports. I was asking why it's a syntax error specifically for
relative imports.

George



More information about the Python-list mailing list