473,386 Members | 1,803 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

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

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

George
Jan 14 '08 #1
4 4556
George Sakkis <ge***********@gmail.comwrites:
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.

--
\ "Never use a long word when there's a commensurate diminutive |
`\ available." -- Stan Kelly-Bootle |
_o__) |
Ben Finney
Jan 14 '08 #2
On Jan 14, 6:01 pm, Ben Finney <bignose+hates-s...@benfinney.id.au>
wrote:
George Sakkis <george.sak...@gmail.comwrites:
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
Jan 14 '08 #3
On Jan 14, 2:37 pm, George Sakkis <george.sak...@gmail.comwrote:
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
Jan 14 '08 #4
George Sakkis wrote:
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
Jan 15 '08 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: j vickroy | last post by:
Could someone help me understand the following Python 2.3 error message: SyntaxError: unqualified exec is not allowed in function 'load' it contains a nested function with free variables in...
3
by: Attila Szabo | last post by:
Hi, I wrote this sample piece of code: def main(): lambda x: 'ABC%s' % str(x) for k in range(2): exec('print %s' % k) main()
2
by: Victor Engmark | last post by:
I am making an XML Schema for emails, and would like to specify that any elements from the XHTML2 namespace are allowed in the body/contents and signature/footer parts of the message (and _only_...
1
by: Joe | last post by:
Hello All, I am trying to insert a record in the MS Access DB and for some reason I cannot get rid of error message, System.Data.OleDb.OleDbException: Syntax error in INSERT INTO statement. ...
1
by: Todd | last post by:
Hi, I'm working right from the example here to make a basic extenstion module. http://docs.python.org/ext/intro.html...
5
by: DIBS | last post by:
I'm new to Python and I don't understand what I'm doing wrong. I'm running windows xp. In the command line window, I type: Python Sudoku.py and I get the response" SyntaxError: invalid...
10
by: ronrsr | last post by:
no matter where I place this imported file,the statement after it in the main program gets a syntax error, regardless of the syntax. I think I may have changed something in this file, but I'm...
7
by: oscartheduck | last post by:
I have a small script for doing some ssh stuff for me. I could have written it as shell script, but wanted to improve my python skills some. RIght now, I'm not catching a syntax error as I'd...
1
by: neridaj | last post by:
Hello, I'm going through the SOAP Web Services portion of Mark Pilgrim's tutorial and I'm getting this error when trying to build: python setup.py build Traceback (most recent call last):...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.