[New-bugs-announce] [issue30840] Contrary to documentation, relative imports cannot pass through the top level

Malcolm Smith report at bugs.python.org
Mon Jul 3 15:14:52 EDT 2017


New submission from Malcolm Smith:

https://docs.python.org/3/reference/simple_stmts.html#the-import-statement defers the full specification of relative imports to PEP 328. PEP 328 gives the example "from ...sys import path" in a second-level package, and notes "while that last case is legal, it is certainly discouraged".

However, in the current implementation it actually isn't legal. Using a stdlib second-level package to test:

Python 3.5.3 (default, Apr 10 2017, 07:53:16)  [GCC 6.3.0 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import xml.dom
>>> __package__ = "xml.dom"
>>> from . import minidom  # Works fine
>>> from ...sys import path
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: attempted relative import beyond top-level package

Either the documentation or the implementation should be fixed.

----------
components: Interpreter Core
messages: 297608
nosy: Malcolm Smith
priority: normal
severity: normal
status: open
title: Contrary to documentation, relative imports cannot pass through the top level
type: behavior
versions: Python 3.5

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue30840>
_______________________________________


More information about the New-bugs-announce mailing list