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: Probably incorrect message after failed import
Type: Stage: resolved
Components: Interpreter Core Versions: Python 3.7
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: steven.daprano, vanrein
Priority: normal Keywords:

Created on 2020-02-15 11:23 by vanrein, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Messages (3)
msg362011 - (view) Author: Rick van Rein (vanrein) Date: 2020-02-15 11:23
The following error message surprises me:

>>> import os.environ
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'os.environ'; 'os' is not a package

Shouldn't that say that "'environ' is not a package" instead?

After all, 'os' will support

>>> import os.path
>>> 

This is confusing :)
msg362015 - (view) Author: Steven D'Aprano (steven.daprano) * (Python committer) Date: 2020-02-15 12:08
> Shouldn't that say that "'environ' is not a package" instead?

No. The os module is special, so I'll talk about the usual case first.

Normally, for `import spam.eggs` to succeed, *spam* has to be a package, and *eggs* has to be either a sub-module inside that package, or a sub-package. So the general error message is correct. You can't normally use the dotted import syntax to import functions from a module.

But the `os` module is special: it is very old, pre-dating the invention of packages, and the `os` module fakes a pretend package for `os.path` by doing this:

    sys.modules['os.path'] = path


So even though `os` is not a package, you can still use dotted package syntax to import `os.path`.

I guess this was intended as a convenience, without considering how this could be confusing.
msg362017 - (view) Author: Rick van Rein (vanrein) Date: 2020-02-15 13:14
Thanks for explaining.  It is indeed confusing, even though I'm quite experienced with Python.
History
Date User Action Args
2022-04-11 14:59:26adminsetgithub: 83818
2020-02-15 13:15:44vanreinsetstatus: open -> closed
resolution: not a bug
stage: resolved
2020-02-15 13:14:57vanreinsetmessages: + msg362017
2020-02-15 12:08:27steven.dapranosetnosy: + steven.daprano
messages: + msg362015
2020-02-15 11:23:56vanreincreate