pFad - Phone/Frame/Anonymizer/Declutterfier! Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

URL: http://github.com/python/cpython/commit/70bf713617e15fad390ed953e48b3c65d9bc90ec

8faa60c69660fa.css" /> bpo-30840: Document relative imports (#12831) Β· python/cpython@70bf713 Β· GitHub
Skip to content

Commit 70bf713

Browse files
nanjekyejoannahncoghlan
authored andcommitted
bpo-30840: Document relative imports (#12831)
* document relative imports * πŸ“œπŸ€– Added by blurb_it. * fix indentation error * remove indentation * Document relative imports * Document relative imports * remove from ...package * Document relative imports * remove trailing space * Document relative imports * Document relative imports
1 parent b930a2d commit 70bf713

3 files changed

Lines changed: 43 additions & 1 deletion

File tree

β€ŽDoc/reference/import.rstβ€Ž

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -921,6 +921,46 @@ it is sufficient to raise :exc:`ModuleNotFoundError` directly from
921921
``None``. The latter indicates that the meta path search should continue,
922922
while raising an exception terminates it immediately.
923923

924+
.. _relativeimports:
925+
926+
Package Relative Imports
927+
========================
928+
929+
Relative imports use leading dots. A single leading dot indicates a relative
930+
import, starting with the current package. Two or more leading dots indicate a
931+
relative import to the parent(s) of the current package, one level per dot
932+
after the first. For example, given the following package layout::
933+
934+
package/
935+
__init__.py
936+
subpackage1/
937+
__init__.py
938+
moduleX.py
939+
moduleY.py
940+
subpackage2/
941+
__init__.py
942+
moduleZ.py
943+
moduleA.py
944+
945+
In either ``subpackage1/moduleX.py`` or ``subpackage1/__init__.py``,
946+
the following are valid relative imports::
947+
948+
from .moduleY import spam
949+
from .moduleY import spam as ham
950+
from . import moduleY
951+
from ..subpackage1 import moduleY
952+
from ..subpackage2.moduleZ import eggs
953+
from ..moduleA import foo
954+
955+
Absolute imports may use either the ``import <>`` or ``from <> import <>``
956+
syntax, but relative imports may only use the second form; the reason
957+
for this is that::
958+
959+
import XXX.YYY.ZZZ
960+
961+
should expose ``XXX.YYY.ZZZ`` as a usable expression, but .moduleY is
962+
not a valid expression.
963+
924964

925965
Special considerations for __main__
926966
===================================

β€ŽDoc/reference/simple_stmts.rstβ€Ž

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -828,7 +828,8 @@ exists. Two dots means up one package level. Three dots is up two levels, etc.
828828
So if you execute ``from . import mod`` from a module in the ``pkg`` package
829829
then you will end up importing ``pkg.mod``. If you execute ``from ..subpkg2
830830
import mod`` from within ``pkg.subpkg1`` you will import ``pkg.subpkg2.mod``.
831-
The specification for relative imports is contained within :pep:`328`.
831+
The specification for relative imports is contained in
832+
the :ref:`relativeimports` section.
832833

833834
:func:`importlib.import_module` is provided to support applications that
834835
determine dynamically the modules to be loaded.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Document relative imports

0 commit comments

Comments
Β (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad © 2024 Your Company Name. All rights reserved.





Check this box to remove all script contents from the fetched content.



Check this box to remove all images from the fetched content.


Check this box to remove all CSS styles from the fetched content.


Check this box to keep images inefficiently compressed and original size.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy