@@ -519,7 +519,10 @@ the module.
519519
520520 The ``__spec__ `` attribute must be set to the module spec that was
521521 used when importing the module. This is used primarily for
522- introspection and during reloading.
522+ introspection and during reloading. Setting ``__spec__ ``
523+ appropriately applies equally to :ref: `modules initialized during
524+ interpreter startup <programs>`. The one exception is ``__main__ ``,
525+ where ``__spec__ `` is :ref: `set to None in some cases <main_spec >`.
523526
524527 .. versionadded :: 3.4
525528
@@ -829,6 +832,37 @@ it is sufficient to raise :exc:`ImportError` directly from
829832while raising an exception terminates it immediately.
830833
831834
835+ Special considerations for __main__
836+ ===================================
837+
838+ The :mod: `__main__ ` module is a special case relative to Python's import
839+ system. As noted :ref: `elsewhere <programs >`, the ``__main__ `` module
840+ is directly initialized at interpreter startup, much like :mod: `sys ` and
841+ :mod: `builtins `. However, unlike those two, it doesn't strictly
842+ qualify as a built-in module. This is because the manner in which
843+ ``__main__ `` is initialized depends on the flags and other options with
844+ which the interpreter is invoked.
845+
846+ .. _main_spec :
847+
848+ __main__.__spec__
849+ -----------------
850+
851+ Depending on how :mod: `__main__ ` is initialized, ``__main__.__spec__ ``
852+ gets set appropriately or to ``None ``.
853+
854+ When Python is started with the :option: `-m ` option, ``__spec__ `` is set
855+ to the module spec of the corresponding module.
856+
857+ In :ref: `the remaining cases <using-on-interface-options >`
858+ ``__main__.__spec__ `` is set to ``None ``:
859+
860+ - interactive prompt
861+ - -c switch
862+ - running from stdin
863+ - running directly from a source or bytecode file
864+
865+
832866Open issues
833867===========
834868
@@ -841,6 +875,12 @@ related entries in the data model reference page?
841875XXX runpy, pkgutil, et al in the library manual should all get "See Also"
842876links at the top pointing to the new import system section.
843877
878+ XXX Add more explanation regarding the different ways in which
879+ ``__main__ `` is initialized?
880+
881+ XXX Add more info on ``__main__ `` quirks/pitfalls (i.e. copy from
882+ :pep: `395 `).
883+
844884
845885References
846886==========
0 commit comments