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.

Author serhiy.storchaka
Recipients Maciej Gol, brett.cannon, eric.snow, ethan.furman, lars.gustaebel, ncoghlan, serhiy.storchaka
Date 2020-01-24.14:08:24
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1579874904.2.0.992346888571.issue39430@roundup.psfhosted.org>
In-reply-to
Content
It is intended to support circular imports. Let foo.py contains "import bar" and bar.py contains "import foo". When you execute "import foo", the import machinery first creates an empty module foo, adds it to sys.modules, reads foo.py and executes it in the namespace of module foo. When the interpreter encounters "import bar" in foo.py, the import machinery creates an empty module bar, adds it to sys.modules, reads bar.py and executes it in the namespace of module bar. When the interpreter encounters "import foo" in bar.py, the import machinery takes the module foo from sys.modules. So you break an infinite cycle and can import modules with cyclic dependencies.

You can argue that cyclic import does not look as a good practice, but actually it is pretty common case when you import a submodule in a package. If foo/__init__.py contains "from .bar import Bar", the foo module must be imported before you import foo.bar, but is not completely initialized at that time yet.
History
Date User Action Args
2020-01-24 14:08:24serhiy.storchakasetrecipients: + serhiy.storchaka, brett.cannon, ncoghlan, lars.gustaebel, ethan.furman, eric.snow, Maciej Gol
2020-01-24 14:08:24serhiy.storchakasetmessageid: <1579874904.2.0.992346888571.issue39430@roundup.psfhosted.org>
2020-01-24 14:08:24serhiy.storchakalinkissue39430 messages
2020-01-24 14:08:24serhiy.storchakacreate