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: Strange behavior during invalid import of modules without if __name__ == "__main__"
Type: behavior Stage: resolved
Components: Library (Lib) Versions: Python 3.9, Python 3.8, Python 3.7
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: rhettinger, wyz23x2
Priority: normal Keywords:

Created on 2020-04-25 03:57 by wyz23x2, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Messages (3)
msg367253 - (view) Author: wyz23x2 (wyz23x2) * Date: 2020-04-25 03:57
This behavior:

Python 3.8.2 (tags/v3.8.2:7b3ab59, Feb 25 2020, 23:03:10) [MSC v.1916 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license()" for more information.
>>> import this.main
The Zen of Python, by Tim Peters

Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let's do more of those!
Traceback (most recent call last):
  File "<pyshell#0>", line 1, in <module>
    import this.main
ModuleNotFoundError: No module named 'this.main'; 'this' is not a package
>>> import this.main
Traceback (most recent call last):
  File "<pyshell#1>", line 1, in <module>
    import this.main
ModuleNotFoundError: No module named 'this.main'; 'this' is not a package
>>> del this
Traceback (most recent call last):
  File "<pyshell#2>", line 1, in <module>
    del this
NameError: name 'this' is not defined
>>> import this
>>>

This confuses users because the code of "this" is un the 1st time, but not the times after it. And "this" isn't actually imported after that; stranger is when you perform the correct import, it doesn't run.

Is this right?
msg367254 - (view) Author: wyz23x2 (wyz23x2) * Date: 2020-04-25 03:59
Sorry, it's "of 'this' is run", not "un".
msg367256 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2020-04-25 04:51
Trying using "import this" instead of "import this.main".

The latter does an "import this" and then attempts to load a "main" package that doesn't exist.  You're observed the expected behavior.  See https://docs.python.org/3/tutorial/modules.html#packages for more details.

Hope that clears-up your understanding of what is happening.
History
Date User Action Args
2022-04-11 14:59:29adminsetgithub: 84566
2020-04-25 04:51:17rhettingersetstatus: open -> closed

nosy: + rhettinger
messages: + msg367256

resolution: not a bug
stage: resolved
2020-04-25 04:03:01wyz23x2settype: behavior
2020-04-25 03:59:35wyz23x2setmessages: + msg367254
2020-04-25 03:57:59wyz23x2setcomponents: + Library (Lib)
versions: + Python 3.7, Python 3.8, Python 3.9
2020-04-25 03:57:29wyz23x2create