File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 55.. module :: __main__
66 :synopsis: The environment where the top-level script is run.
77
8+ '__main__' is the name of the scope in which top-level code executes.
9+ A module's __name__ is set equal to '__main__' when read from
10+ standard input, a script, or from an interactive prompt.
811
9- This module represents the (otherwise anonymous) scope in which the
10- interpreter's main program executes --- commands read either from standard
11- input, from a script file, or from an interactive prompt. It is this
12- environment in which the idiomatic "conditional script" stanza causes a script
13- to run::
12+ A module can discover whether or not it is running in the main scope by
13+ checking its own __name__, which allows a common idiom for conditionally
14+ executing code in a module when it is run as a script or with `python
15+ -m ` but not when it is imported:
1416
1517 if __name__ == "__main__":
18+ # execute only if run as a script
1619 main()
1720
21+ For a package, the same effect can be achieved by including a
22+ __main__.py module, the contents of which will be executed when the
23+ module is run with -m.
You can’t perform that action at this time.
0 commit comments