upadte from Aahz

This commit is contained in:
David Goodger 2004-05-02 16:32:32 +00:00
parent 081033c9a7
commit edae0ba0b1
1 changed files with 57 additions and 32 deletions

View File

@ -41,10 +41,11 @@ In Python 2.4, you must enable the new absolute import behavior with ::
from __future__ import absolute_import
You may use relative imports freely. In Python 2.5, any ``import``
statement that results in an intra-package import will generate a
``PendingDeprecation`` warning (this also applies to ``from <> import``
that fails to use the relative import syntax). In Python 2.6, ``import``
will always be an absolute import.
statement that results in an intra-package import will raise
``DeprecationWarning`` (this also applies to ``from <> import`` that
fails to use the relative import syntax). In Python 2.6, ``import`` will
always be an absolute import (and the ``__future__`` directive will no
longer be needed).
Rationale for Parentheses
@ -71,33 +72,37 @@ mechanism (parentheses) to write the ``import`` statement::
from Tkinter import (Tk, Frame, Button, Entry, Canvas, Text,
LEFT, DISABLED, NORMAL, RIDGE, END)
This part of the proposal already has BDFL approval.
This part of the proposal had BDFL approval from the beginning.
Rationale for Absolute Imports
==============================
In current Python, if you're reading a module located inside a
In Python 2.3 and earlier, if you're reading a module located inside a
package, it is not clear whether ::
import foo
refers to a top-level module or to another module inside the package.
Let's say today it refers to a module internal to the package. Then
tomorrow, the standard library decides to add its own foo package that
you'd like to use. You can't without renaming your internal module.
To resolve these ambiguities, it is proposed that ``foo`` will always be a
module or package reachable from ``sys.path``.
As Python's library expands, more and more existing package internal
modules suddenly shadow standard library modules by accident. It's a
particularly difficult problem inside packages because there's no way to
specify which module is meant. To resolve the ambiguity, it is proposed
that ``foo`` will always be a module or package reachable from
``sys.path``. This is called an absolute import.
The python-dev community chose absolute imports as the default because
they're the more common use case and because absolute imports can provide
all the functionality of relative (intra-package) imports -- albeit at
the cost of difficulty when renaming package pieces higher up in the
hierarchy or when moving one package inside another.
Because this represents a change in semantics, absolute imports will
be optional in Python 2.4 through the use of ::
be optional in Python 2.4 and 2.5 through the use of ::
from __future__ import absolute_import
This PEP will be updated when it is decided to make absolute imports
the default, probably Python 2.5 or 2.6.
This part of the proposal already has BDFL approval.
This part of the proposal had BDFL approval from the beginning.
Rationale for Relative Imports
@ -207,22 +212,28 @@ Here are the contenders:
Guido's Decision
----------------
================
Guido has Pronounced [1]_ that relative imports will use leading dots,
one per level of parent. Further discussion led to the following
clarification of the semantics. Given a package layout::
Guido has Pronounced [1]_ that relative imports will use leading dots.
A single leading dot indicates a relative import, starting with the
current package. Two or more leading dots give a relative import to the
parent(s) of the current package, one level per dot after the first.
Here's a sample package layout::
package
subpackage1
moduleX
moduleY
subpackage2
moduleZ
moduleA
package/
__init__.py
subpackage1/
__init__.py
moduleX.py
moduleY.py
subpackage2/
__init__.py
moduleZ.py
moduleA.py
Assuming that the current file is ``moduleX.py``, following are correct
usages of the new syntax::
Assuming that the current file is either ``moduleX.py`` or
``subpackage1/__init__.py``, following are correct usages of the new
syntax::
from .moduleY import spam
from .moduleY import spam as ham
@ -236,9 +247,23 @@ usages of the new syntax::
Note that while that last case is legal, it is certainly discouraged
("insane" was the word Guido used).
Reminder: relative imports must always use ``from <> import``;
``import <>`` is always absolute. Of course, absolute imports can use
``from <> import`` by omitting the leading dots.
Relative imports must always use ``from <> import``; ``import <>`` is
always absolute. Of course, absolute imports can use ``from <> import``
by omitting the leading dots. The reason ``import .foo`` is prohibited
is because after ::
import XXX.YYY.ZZZ
then ::
XXX.YYY.ZZZ
is usable in an expression. But ::
.moduleY
is not usable in an expression.