]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
bpo-30840: Document relative imports (GH-12831) (GH-12938)
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Wed, 24 Apr 2019 15:44:19 +0000 (08:44 -0700)
committerNick Coghlan <ncoghlan@gmail.com>
Wed, 24 Apr 2019 15:44:19 +0000 (01:44 +1000)
* Document relative imports
(cherry picked from commit 70bf713617e15fad390ed953e48b3c65d9bc90ec)

Co-authored-by: Joannah Nanjekye <33177550+nanjekyejoannah@users.noreply.github.com>
Doc/reference/import.rst
Doc/reference/simple_stmts.rst
Misc/NEWS.d/next/Documentation/2019-04-14-19-46-21.bpo-30840.R-JFzw.rst [new file with mode: 0644]

index 9a0ab39d3b4a3ef8f2c458d4ea7090f1b8b02c9e..88290c88bb35d9cbd020899eabcd9842b9f23a01 100644 (file)
@@ -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__
 ===================================
index 07072d954384060e599ea1f35fa75726708b17fa..1779832a9fdd6928016a0db47e518d3d3c0d3b57 100644 (file)
@@ -829,7 +829,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.
diff --git a/Misc/NEWS.d/next/Documentation/2019-04-14-19-46-21.bpo-30840.R-JFzw.rst b/Misc/NEWS.d/next/Documentation/2019-04-14-19-46-21.bpo-30840.R-JFzw.rst
new file mode 100644 (file)
index 0000000..210f54f
--- /dev/null
@@ -0,0 +1 @@
+Document relative imports
\ No newline at end of file