This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: [doc] Tutorial incorrectly claims that (explicit) relative imports don't work in the main module
Type: behavior Stage:
Components: Documentation Versions: Python 3.11, Python 3.10, Python 3.9
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: docs@python Nosy List: Kevin.Norris, docs@python, ezio.melotti, martin.panter
Priority: normal Keywords:

Created on 2016-01-20 07:02 by Kevin.Norris, last changed 2022-04-11 14:58 by admin.

Messages (3)
msg258650 - (view) Author: Kevin Norris (Kevin.Norris) Date: 2016-01-20 07:02
The tutorial contains this statement:

Note that relative imports are based on the name of the current module. Since the name of the main module is always "__main__", modules intended for use as the main module of a Python application must always use absolute imports.

See <https://docs.python.org/3/tutorial/modules.html#intra-package-references>.  The wording is slightly different in the 2.7 version of the tutorial to account for the existence of implicit relative imports, but it clearly states that explicit relative imports suffer from this limitation.

As of PEP 366, this is no longer true.  You can do (explicit) relative imports in the main module just fine (though with some minor caveats w.r.t. using the -m flag vs. specifying the .py file directly).  PEP 366 has a Python-Version of 2.6, 3.0, so every sufficiently recent version of the tutorial is wrong.

It's probably not a good idea to get too far into those caveats in the tutorial.  I'd suggest wording like this:

Note that relative imports are based on the name of the current package.  If Python is invoked with the -m switch, it can determine the package name automatically, but if it is invoked directly as ``python file.py``, relative imports will not work because Python doesn't know what package file.py belongs to.

It might be worth mentioning __package__ here, too, but we don't want to get too far down the rabbit hole of trivia (e.g. PEP 366 discusses sys.path manipulation, and for that you probably want to use __file__ instead of hard-coding the path, and then you have to talk about zipimports, and then, and then, and then...!).

(For the record, is the 2.7 tutorial still under active support?  Is it okay to report bugs in it?)
msg258659 - (view) Author: Martin Panter (martin.panter) * (Python committer) Date: 2016-01-20 10:29
Yes 2.7 is open for changes to the documentation like this, as is 3.5+.

It’s been a while since I looked at the tutorial. Does it even mention the “python -m” mode? I don’t think this is the right place to introduce it.

Perhaps it would be okay to just say relative imports are based on the _full_ name of the current module. It could be misleading saying “the name of the current package”, because the import could be relative to the a higher level parent package if more than one dot is specified. Then point out that relative imports won’t work with ``python file.py`` or interactive mode, but don’t mention “-m” (or the other ways to run scripts and modules).
msg258878 - (view) Author: Kevin Norris (Kevin.Norris) Date: 2016-01-24 00:45
>It could be misleading saying “the name of the current package”, because the import could be relative to the a higher level parent package if more than one dot is specified.

While this is correct, Python still uses __package__ to determine what to import in that case.  I simply replaced "__package__" with "the name of the current package" to make it easier to read, much as the original text replaces "__name__" with "the name of the current module."

>Then point out that relative imports won’t work with ``python file.py`` or interactive mode, but don’t mention “-m” (or the other ways to run scripts and modules).

SGTM if you can find a reasonable way of phrasing that.
History
Date User Action Args
2022-04-11 14:58:26adminsetgithub: 70348
2021-12-03 23:23:09iritkatrielsettitle: Tutorial incorrectly claims that (explicit) relative imports don't work in the main module -> [doc] Tutorial incorrectly claims that (explicit) relative imports don't work in the main module
versions: + Python 3.9, Python 3.10, Python 3.11, - Python 2.7, Python 3.5, Python 3.6
2016-01-28 13:07:32ezio.melottisetnosy: + ezio.melotti
2016-01-24 00:45:27Kevin.Norrissetmessages: + msg258878
2016-01-20 10:29:46martin.pantersetnosy: + martin.panter

messages: + msg258659
versions: - Python 3.2, Python 3.3, Python 3.4
2016-01-20 07:02:32Kevin.Norriscreate