Skip to content

bpo-39452: rewrite and expand __main__.rst #26883

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 51 commits into from
Aug 24, 2021
Merged
Changes from 1 commit
Commits
Show all changes
51 commits
Select commit Hold shift + click to select a range
02e9edf
__main__docs: intro and first secton
jdevries3133 Jun 21, 2021
c95f69b
bpo-44494: rewrite of Doc/library/__main__.rst (first draft)
jdevries3133 Jun 23, 2021
235e866
bpo-44494: add blurb
jdevries3133 Jun 23, 2021
a292ab6
Update __main__.rst
geryogam Jun 30, 2019
d7a1999
Update __main__.rst
geryogam Jun 30, 2019
8398f08
Take Steven d’Aprano’s review into account
geryogam Sep 16, 2020
1fcb2af
Rewrap lines
geryogam Sep 16, 2020
2bde063
Remove trailing whitespaces
geryogam Sep 17, 2020
d29fd2a
bpo-39452: rewrite and expansion of __main__.rst
jdevries3133 Jun 23, 2021
4c60f2c
mention runpy
jdevries3133 Jun 29, 2021
2b5f710
add "design patterns" section, fix section title hierarchies
jdevries3133 Jun 29, 2021
7e495d7
add sentence about console_scripts
jdevries3133 Jun 29, 2021
56afeaa
misc formatting; change "Design Patterns" to "Idiomatic Usage"
jdevries3133 Jun 30, 2021
2a398ef
add section about sys.exit(main()) convention
jdevries3133 Jun 30, 2021
1a9956c
make last paragraph 'idiomatic usage'; add comment about maybe deleting
jdevries3133 Jun 30, 2021
c4b5cea
fix linting error (default context used in comment)
jdevries3133 Jun 30, 2021
1f012b4
revise example so that main() does not take arguments
jdevries3133 Jun 30, 2021
f095362
add console_scripts section, remove bad old example
jdevries3133 Jun 30, 2021
c42b706
minor proofreading changes
jdevries3133 Jun 30, 2021
7fe7f1c
implement changes suggest by @merwok
jdevries3133 Jun 30, 2021
c063da1
fix: typos
jdevries3133 Jun 30, 2021
7c6b451
fix wording, slim down example, add reference to relative import docs
jdevries3133 Jul 7, 2021
06bcb09
respond to review from @pradyunsg
jdevries3133 Jul 21, 2021
80756b3
Merge remote-tracking branch 'upstream/main' into bpo-39452__main__docs
jdevries3133 Jul 31, 2021
647c471
add `import __main__` section
jdevries3133 Aug 1, 2021
457bbc9
revisions and proofreading
jdevries3133 Aug 1, 2021
3d9b3b9
eliminate opinionated section about idiomatic usage of `__main__.py`
jdevries3133 Aug 1, 2021
dd68513
proofread `__main__.py` section
jdevries3133 Aug 1, 2021
6ee7090
Merge branch 'main' of github.com:python/cpython into bpo-39452__main…
jdevries3133 Aug 4, 2021
757b03a
incorporate suggested changes from @Fidget-Spinner
jdevries3133 Aug 10, 2021
8e86468
incorporate suggested changes from @yaseppochi
jdevries3133 Aug 10, 2021
f33a081
fix formatting
jdevries3133 Aug 10, 2021
14bad85
name equals main
jdevries3133 Aug 12, 2021
d150674
Merge branch 'main' of github.com:python/cpython into bpo-39452__main…
jdevries3133 Aug 12, 2021
7b987cb
Merge branch 'bpo-39452__main__docs' of github.com:jdevries3133/cpyth…
jdevries3133 Aug 12, 2021
b9db705
also change reference to name equals main section
jdevries3133 Aug 12, 2021
168c774
implement feedback from @holdenweb, python-dev, and @merwork
jdevries3133 Aug 12, 2021
c450171
fix trailing whitespace
jdevries3133 Aug 12, 2021
077e7a4
Remove .bak file
ambv Aug 24, 2021
eb42489
Thorough editing pass
ambv Aug 24, 2021
7c61e78
Move `__main__.py` section above the `import __main__` section
ambv Aug 24, 2021
45a9425
s/command line/command-line/
ambv Aug 24, 2021
073e9d7
Mention asyncio.__main__
ambv Aug 24, 2021
ccb9004
Restore proper document name
ambv Aug 24, 2021
f8630fa
Use proper .. seealso:: sections.
ambv Aug 24, 2021
1e86e02
Appease `make suspicious`
ambv Aug 24, 2021
9c87442
Spell out examples of top-level code environments
ambv Aug 24, 2021
0d4fc8a
Appease double dot alignment aesthetics
ambv Aug 24, 2021
4e51333
Replace lies with truth
ambv Aug 24, 2021
6f0f82c
Improve flow introducing what "top-level code environment" is
ambv Aug 24, 2021
46e7668
Use module names consistently in example
ambv Aug 24, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
__main__docs: intro and first secton
  • Loading branch information
jdevries3133 committed Jun 21, 2021
commit 02e9edfbe366a5ce7c53965848638b2893f80245
99 changes: 95 additions & 4 deletions Doc/library/__main__.rst
Original file line number Diff line number Diff line change
@@ -1,12 +1,103 @@

:mod:`__main__` --- Top-level script environment
================================================
:mod:`__main__` --- CLIs, import-time behavior, and ``__name__ == ‘__main__’``
=================================================================================

.. module:: __main__
:synopsis: The environment where the top-level script is run.
:synopsis: CLIs, import-time behavior, and ``__name__ == ‘__main__’``

--------------

The concept of ``__main__`` in Python can be confusing at first. It is not a
single mechanism in the language, but in fact is part of two quite different
constructs:

1. The ``__main__.py`` file in a Python module; see :ref:`___main__.py`.
2. The very common ``__name__ == '__main__'`` construct.

The term ``__main__`` in both of these use cases does indeed point towards a
common design philosophy, which hopefully will become clear. Nonetheless, each
will be documented separately in the following two sections.


.. ___main__.py:

``__main__.py`` in Python Packages
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

If you are not familiar with Python packages, see section :ref:`tut-packages`.
Most commonly, the :file:`__main__.py` file in a Python package is used to
provide a command line interface (CLI) for that package. Consider the
following hypothetical package, "bandclass"::

project_root
└── bandclass
├── __init__.py
├── __main__.py
├── parent.py
├── section.py
└── student.py

``__main__.py`` will be excecuted when the package itself is invoked directly
from the command line using the ``-m`` flag. For example::

python3 -m bandclass

In this example, ``__main__.py`` might be used to provide a CLI for the
teacher to get information. In combination with :mod:`argparse`,
``__main__.py`` becomes a beautiful way to add command line functionality
to your packages.

For a very popular example of this usage pattern in our standard library,
see :mod:`http.server`, and its' invocation via
``python3 -m http.server [directory]``









.. REDRAFT PLAN

There have been many complaints about the shortcoming of the documentation
towards informing users about __main__. Both the popular __name__ == '__main__' construct, and the role of __main__.py in a python module.

bpo-17359
bpo-24632
bpo-38452

I propose a broad overhaul of Doc/library/__main__.rst to address these
shortcomings and to provide a single source of truth on __main__ (in
general!). This is an appropriate place to put this information.
Both the __name__ == '__main__' and fooModule/__main__.py
constructs reasonably fall under the category of “Python Runtime Services,”
because they both control the way that programs run depending on how they are
used (command-line versus import versus running directly).

The new Doc/library/__main__.rst should have a new synopsis of, “CLIs,
import-time behavior, and if __name__ == ‘__main__’”, reflecting its new and
broader focus.

Additionally, the new docs should have the following distinct sections:

Differentiating between __name__ == ‘__main__’ and __main.__.py
__main__.py and the -m flag (this is roughly what is there already, although
it’s not as descriptive as it should be).
__name__ and the if __name__ == '__main__' construct.

If there is interest, I would be happy to open uptake this work on as soon as there is
consensus around this plan. I’m looking forward to hearing what you think!







.. OLD DOCUMENTATION


``'__main__'`` is the name of the scope in which top-level code executes.
A module's __name__ is set equal to ``'__main__'`` when read from
standard input, a script, or from an interactive prompt.
Expand Down
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy