upadte from Aahz
This commit is contained in:
parent
081033c9a7
commit
edae0ba0b1
89
pep-0328.txt
89
pep-0328.txt
|
@ -41,10 +41,11 @@ In Python 2.4, you must enable the new absolute import behavior with ::
|
||||||
from __future__ import absolute_import
|
from __future__ import absolute_import
|
||||||
|
|
||||||
You may use relative imports freely. In Python 2.5, any ``import``
|
You may use relative imports freely. In Python 2.5, any ``import``
|
||||||
statement that results in an intra-package import will generate a
|
statement that results in an intra-package import will raise
|
||||||
``PendingDeprecation`` warning (this also applies to ``from <> import``
|
``DeprecationWarning`` (this also applies to ``from <> import`` that
|
||||||
that fails to use the relative import syntax). In Python 2.6, ``import``
|
fails to use the relative import syntax). In Python 2.6, ``import`` will
|
||||||
will always be an absolute import.
|
always be an absolute import (and the ``__future__`` directive will no
|
||||||
|
longer be needed).
|
||||||
|
|
||||||
|
|
||||||
Rationale for Parentheses
|
Rationale for Parentheses
|
||||||
|
@ -71,33 +72,37 @@ mechanism (parentheses) to write the ``import`` statement::
|
||||||
from Tkinter import (Tk, Frame, Button, Entry, Canvas, Text,
|
from Tkinter import (Tk, Frame, Button, Entry, Canvas, Text,
|
||||||
LEFT, DISABLED, NORMAL, RIDGE, END)
|
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
|
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 ::
|
package, it is not clear whether ::
|
||||||
|
|
||||||
import foo
|
import foo
|
||||||
|
|
||||||
refers to a top-level module or to another module inside the package.
|
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
|
As Python's library expands, more and more existing package internal
|
||||||
tomorrow, the standard library decides to add its own foo package that
|
modules suddenly shadow standard library modules by accident. It's a
|
||||||
you'd like to use. You can't without renaming your internal module.
|
particularly difficult problem inside packages because there's no way to
|
||||||
To resolve these ambiguities, it is proposed that ``foo`` will always be a
|
specify which module is meant. To resolve the ambiguity, it is proposed
|
||||||
module or package reachable from ``sys.path``.
|
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
|
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
|
from __future__ import absolute_import
|
||||||
|
|
||||||
This PEP will be updated when it is decided to make absolute imports
|
This part of the proposal had BDFL approval from the beginning.
|
||||||
the default, probably Python 2.5 or 2.6.
|
|
||||||
|
|
||||||
This part of the proposal already has BDFL approval.
|
|
||||||
|
|
||||||
|
|
||||||
Rationale for Relative Imports
|
Rationale for Relative Imports
|
||||||
|
@ -207,22 +212,28 @@ Here are the contenders:
|
||||||
|
|
||||||
|
|
||||||
Guido's Decision
|
Guido's Decision
|
||||||
----------------
|
================
|
||||||
|
|
||||||
Guido has Pronounced [1]_ that relative imports will use leading dots,
|
Guido has Pronounced [1]_ that relative imports will use leading dots.
|
||||||
one per level of parent. Further discussion led to the following
|
A single leading dot indicates a relative import, starting with the
|
||||||
clarification of the semantics. Given a package layout::
|
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
|
package/
|
||||||
subpackage1
|
__init__.py
|
||||||
moduleX
|
subpackage1/
|
||||||
moduleY
|
__init__.py
|
||||||
subpackage2
|
moduleX.py
|
||||||
moduleZ
|
moduleY.py
|
||||||
moduleA
|
subpackage2/
|
||||||
|
__init__.py
|
||||||
|
moduleZ.py
|
||||||
|
moduleA.py
|
||||||
|
|
||||||
Assuming that the current file is ``moduleX.py``, following are correct
|
Assuming that the current file is either ``moduleX.py`` or
|
||||||
usages of the new syntax::
|
``subpackage1/__init__.py``, following are correct usages of the new
|
||||||
|
syntax::
|
||||||
|
|
||||||
from .moduleY import spam
|
from .moduleY import spam
|
||||||
from .moduleY import spam as ham
|
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
|
Note that while that last case is legal, it is certainly discouraged
|
||||||
("insane" was the word Guido used).
|
("insane" was the word Guido used).
|
||||||
|
|
||||||
Reminder: relative imports must always use ``from <> import``;
|
Relative imports must always use ``from <> import``; ``import <>`` is
|
||||||
``import <>`` is always absolute. Of course, absolute imports can use
|
always absolute. Of course, absolute imports can use ``from <> import``
|
||||||
``from <> import`` by omitting the leading dots.
|
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.
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue