âšī¸ Skipped - page is already crawled
| Filter | Status | Condition | Details |
|---|---|---|---|
| HTTP status | PASS | download_http_code = 200 | HTTP 200 |
| Age cutoff | PASS | download_stamp > now() - 6 MONTH | 2.4 months ago |
| History drop | PASS | isNull(history_drop_reason) | No drop reason |
| Spam/ban | PASS | fh_dont_index != 1 AND ml_spam_score = 0 | ml_spam_score=0 |
| Canonical | PASS | meta_canonical IS NULL OR = '' OR = src_unparsed | Not set |
| Property | Value |
|---|---|
| URL | https://edoras.sdsu.edu/doc/Python-Docs-2.5/whatsnew/pep-328.html |
| Last Crawled | 2026-01-26 19:16:34 (2 months ago) |
| First Indexed | 2017-10-01 19:52:42 (8 years ago) |
| HTTP Status Code | 200 |
| Meta Title | 4 PEP 328: Absolute and Relative Imports |
| Meta Description | null |
| Meta Canonical | null |
| Boilerpipe Text | The simpler part of PEP 328 was implemented in Python 2.4: parentheses
could now be used to enclose the names imported from a module using
the
from ... import ...
statement, making it easier to import
many different names.
The more complicated part has been implemented in Python 2.5:
importing a module can be specified to use absolute or
package-relative imports. The plan is to move toward making absolute
imports the default in future versions of Python.
Let's say you have a package directory like this:
pkg/
pkg/__init__.py
pkg/main.py
pkg/string.py
This defines a package named
pkg
containing the
pkg.main
and
pkg.string
submodules.
Consider the code in the
main.py
module. What happens if it
executes the statement
import string
? In Python 2.4 and
earlier, it will first look in the package's directory to perform a
relative import, finds
pkg/string.py
, imports the contents of
that file as the
pkg.string
module, and that module is bound
to the name "
string
" in the
pkg.main
module's namespace.
That's fine if
pkg.string
was what you wanted. But what if
you wanted Python's standard
string
module? There's no clean
way to ignore
pkg.string
and look for the standard module;
generally you had to look at the contents of
sys.modules
, which
is slightly unclean.
Holger Krekel's
py.std
package provides a tidier way to perform
imports from the standard library,
import py ; py.std.string.join()
,
but that package isn't available on all Python installations.
Reading code which relies on relative imports is also less clear,
because a reader may be confused about which module,
string
or
pkg.string
, is intended to be used. Python users soon
learned not to duplicate the names of standard library modules in the
names of their packages' submodules, but you can't protect against
having your submodule's name being used for a new module added in a
future version of Python.
In Python 2.5, you can switch
import
's behaviour to
absolute imports using a
from __future__ import absolute_import
directive. This absolute-import behaviour will become the default in
a future version (probably Python 2.7). Once absolute imports
are the default,
import string
will
always find the standard library's version.
It's suggested that users should begin using absolute imports as much
as possible, so it's preferable to begin writing
from pkg import
string
in your code.
Relative imports are still possible by adding a leading period
to the module name when using the
from ... import
form:
# Import names from pkg.string
from .string import name1, name2
# Import pkg.string
from . import string
This imports the
string
module relative to the current
package, so in
pkg.main
this will import
name1
and
name2
from
pkg.string
. Additional leading periods
perform the relative import starting from the parent of the current
package. For example, code in the
A.B.C
module can do:
from . import D # Imports A.B.D
from .. import E # Imports A.E
from ..F import G # Imports A.F.G
Leading periods cannot be used with the
import
modname
form of the import statement, only the
from ... import
form.
Release 1.0.
See
About this document...
for information on suggesting changes. |
| Markdown | | | | | | | | |
|---|---|---|---|---|---|---|
| [](https://edoras.sdsu.edu/doc/Python-Docs-2.5/whatsnew/pep-314.html "3 pep 314: Metadata") | [](https://edoras.sdsu.edu/doc/Python-Docs-2.5/whatsnew/whatsnew25.html "What's new in Python") | [](https://edoras.sdsu.edu/doc/Python-Docs-2.5/whatsnew/pep-338.html "5 pep 338: Executing") | What's New in Python 2.5 | [](https://edoras.sdsu.edu/doc/Python-Docs-2.5/whatsnew/contents.html "Table of Contents") |  |  |
**Previous:** [3 PEP 314: Metadata](https://edoras.sdsu.edu/doc/Python-Docs-2.5/whatsnew/pep-314.html) **Up:** [What's New in Python](https://edoras.sdsu.edu/doc/Python-Docs-2.5/whatsnew/whatsnew25.html) **Next:** [5 PEP 338: Executing](https://edoras.sdsu.edu/doc/Python-Docs-2.5/whatsnew/pep-338.html)
***
# 4 PEP 328: Absolute and Relative Imports
The simpler part of PEP 328 was implemented in Python 2.4: parentheses could now be used to enclose the names imported from a module using the `from ... import ...` statement, making it easier to import many different names.
The more complicated part has been implemented in Python 2.5: importing a module can be specified to use absolute or package-relative imports. The plan is to move toward making absolute imports the default in future versions of Python.
Let's say you have a package directory like this:
```
pkg/
pkg/__init__.py
pkg/main.py
pkg/string.py
```
This defines a package named pkg containing the pkg.main and pkg.string submodules.
Consider the code in the main.py module. What happens if it executes the statement `import string`? In Python 2.4 and earlier, it will first look in the package's directory to perform a relative import, finds pkg/string.py, imports the contents of that file as the pkg.string module, and that module is bound to the name "string" in the pkg.main module's namespace.
That's fine if pkg.string was what you wanted. But what if you wanted Python's standard string module? There's no clean way to ignore pkg.string and look for the standard module; generally you had to look at the contents of `sys.modules`, which is slightly unclean. Holger Krekel's py.std package provides a tidier way to perform imports from the standard library, `import py ; py.std.string.join()`, but that package isn't available on all Python installations.
Reading code which relies on relative imports is also less clear, because a reader may be confused about which module, string or pkg.string, is intended to be used. Python users soon learned not to duplicate the names of standard library modules in the names of their packages' submodules, but you can't protect against having your submodule's name being used for a new module added in a future version of Python.
In Python 2.5, you can switch import's behaviour to absolute imports using a `from __future__ import absolute_import` directive. This absolute-import behaviour will become the default in a future version (probably Python 2.7). Once absolute imports are the default, `import string` will always find the standard library's version. It's suggested that users should begin using absolute imports as much as possible, so it's preferable to begin writing in your code.
Relative imports are still possible by adding a leading period to the module name when using the `from ... import` form:
```
# Import names from pkg.string
from .string import name1, name2
# Import pkg.string
from . import string
```
This imports the string module relative to the current package, so in pkg.main this will import name1 and name2 from pkg.string. Additional leading periods perform the relative import starting from the parent of the current package. For example, code in the A.B.C module can do:
```
from . import D # Imports A.B.D
from .. import E # Imports A.E
from ..F import G # Imports A.F.G
```
Leading periods cannot be used with the `import modname` form of the import statement, only the `from ... import` form.
See Also:
[PEP 328, *Imports: Multi-Line and Absolute/Relative*](http://www.python.org/peps/pep-0328.html "Imports: multi-line and Absolute/Relative")
PEP written by Aahz; implemented by Thomas Wouters.
<http://codespeak.net/py/current/doc/index.html>
The py library by Holger Krekel, which contains the
py.std
package.
***
| | | | | | | |
|---|---|---|---|---|---|---|
| [](https://edoras.sdsu.edu/doc/Python-Docs-2.5/whatsnew/pep-314.html "3 pep 314: Metadata") | [](https://edoras.sdsu.edu/doc/Python-Docs-2.5/whatsnew/whatsnew25.html "What's new in Python") | [](https://edoras.sdsu.edu/doc/Python-Docs-2.5/whatsnew/pep-338.html "5 pep 338: Executing") | What's New in Python 2.5 | [](https://edoras.sdsu.edu/doc/Python-Docs-2.5/whatsnew/contents.html "Table of Contents") |  |  |
**Previous:** [3 PEP 314: Metadata](https://edoras.sdsu.edu/doc/Python-Docs-2.5/whatsnew/pep-314.html) **Up:** [What's New in Python](https://edoras.sdsu.edu/doc/Python-Docs-2.5/whatsnew/whatsnew25.html) **Next:** [5 PEP 338: Executing](https://edoras.sdsu.edu/doc/Python-Docs-2.5/whatsnew/pep-338.html)
***
Release 1.0.
See *[About this document...](https://edoras.sdsu.edu/doc/Python-Docs-2.5/whatsnew/about.html)* for information on suggesting changes. |
| Readable Markdown | null |
| Shard | 84 (laksa) |
| Root Hash | 13923086562776807484 |
| Unparsed URL | edu,sdsu!edoras,/doc/Python-Docs-2.5/whatsnew/pep-328.html s443 |