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

G

George Sakkis

Unless I missed it, PEP 328 doesn't mention anything about this.
What's the reason for not allowing "from .relative.module import *' ?

George
 
B

Ben Finney

George Sakkis said:
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.
 
G

George Sakkis

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
 
C

Carl Banks

Unless I missed it, PEP 328 doesn't mention anything about this.
What's the reason for not allowing "from .relative.module import *' ?

I'm just guessing: it could accidentally create infinite recursion.
Or, perhaps something more subtle than infinite recursion, such as
hard-to-comprehend rules about what modules and subpackages would be
imported. So they just decided to disallow it. Just a guess.


Carl Banks
 
S

Steven Bethard

George said:
Unless I missed it, PEP 328 doesn't mention anything about this.
What's the reason for not allowing "from .relative.module import *' ?

Generally, there's a move away from all "import *" versions these days.
For example, Python 3.0 removes the ability to use "import *" within a
function:

http://www.python.org/dev/peps/pep-3100/

I suspect the "import *" version is not enabled for relative imports
because most of python-dev is against "import *" forms anywhere.

STeVe
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top