diff options
author | Joannah Nanjekye <33177550+nanjekyejoannah@users.noreply.github.com> | 2019-04-24 15:14:44 (GMT) |
---|---|---|
committer | Nick Coghlan <ncoghlan@gmail.com> | 2019-04-24 15:14:44 (GMT) |
commit | 70bf713617e15fad390ed953e48b3c65d9bc90ec (patch) | |
tree | 25c32fb5569dcaf9a89bfab02b201388aede6318 /Doc | |
parent | b930a2d2b1247bdba560db341ba90a9cbb538eb3 (diff) | |
download | cpython-70bf713617e15fad390ed953e48b3c65d9bc90ec.zip cpython-70bf713617e15fad390ed953e48b3c65d9bc90ec.tar.gz cpython-70bf713617e15fad390ed953e48b3c65d9bc90ec.tar.bz2 |
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
Diffstat (limited to 'Doc')
-rw-r--r-- | Doc/reference/import.rst | 40 | ||||
-rw-r--r-- | Doc/reference/simple_stmts.rst | 3 |
2 files changed, 42 insertions, 1 deletions
diff --git a/Doc/reference/import.rst b/Doc/reference/import.rst index 9a0ab39..88290c8 100644 --- a/Doc/reference/import.rst +++ b/Doc/reference/import.rst @@ -921,6 +921,46 @@ it is sufficient to raise :exc:`ModuleNotFoundError` directly from ``None``. The latter indicates that the meta path search should continue, while raising an exception terminates it immediately. +.. _relativeimports: + +Package Relative Imports +======================== + +Relative imports use leading dots. A single leading dot indicates a relative +import, starting with the current package. Two or more leading dots indicate a +relative import to the parent(s) of the current package, one level per dot +after the first. For example, given the following package layout:: + + package/ + __init__.py + subpackage1/ + __init__.py + moduleX.py + moduleY.py + subpackage2/ + __init__.py + moduleZ.py + moduleA.py + +In either ``subpackage1/moduleX.py`` or ``subpackage1/__init__.py``, +the following are valid relative imports:: + + from .moduleY import spam + from .moduleY import spam as ham + from . import moduleY + from ..subpackage1 import moduleY + from ..subpackage2.moduleZ import eggs + from ..moduleA import foo + +Absolute imports may use either the ``import <>`` or ``from <> import <>`` +syntax, but relative imports may only use the second form; the reason +for this is that:: + + import XXX.YYY.ZZZ + +should expose ``XXX.YYY.ZZZ`` as a usable expression, but .moduleY is +not a valid expression. + Special considerations for __main__ =================================== diff --git a/Doc/reference/simple_stmts.rst b/Doc/reference/simple_stmts.rst index 00964af..207057c 100644 --- a/Doc/reference/simple_stmts.rst +++ b/Doc/reference/simple_stmts.rst @@ -828,7 +828,8 @@ exists. Two dots means up one package level. Three dots is up two levels, etc. So if you execute ``from . import mod`` from a module in the ``pkg`` package then you will end up importing ``pkg.mod``. If you execute ``from ..subpkg2 import mod`` from within ``pkg.subpkg1`` you will import ``pkg.subpkg2.mod``. -The specification for relative imports is contained within :pep:`328`. +The specification for relative imports is contained in +the :ref:`relativeimports` section. :func:`importlib.import_module` is provided to support applications that determine dynamically the modules to be loaded. |