summaryrefslogtreecommitdiff
path: root/devdocs/python~3.12/library%2F__main__.html
diff options
context:
space:
mode:
authorCraig Jennings <c@cjennings.net>2024-04-07 13:41:34 -0500
committerCraig Jennings <c@cjennings.net>2024-04-07 13:41:34 -0500
commit754bbf7a25a8dda49b5d08ef0d0443bbf5af0e36 (patch)
treef1190704f78f04a2b0b4c977d20fe96a828377f1 /devdocs/python~3.12/library%2F__main__.html
new repository
Diffstat (limited to 'devdocs/python~3.12/library%2F__main__.html')
-rw-r--r--devdocs/python~3.12/library%2F__main__.html122
1 files changed, 122 insertions, 0 deletions
diff --git a/devdocs/python~3.12/library%2F__main__.html b/devdocs/python~3.12/library%2F__main__.html
new file mode 100644
index 00000000..a612efc3
--- /dev/null
+++ b/devdocs/python~3.12/library%2F__main__.html
@@ -0,0 +1,122 @@
+ <span id="main-top-level-code-environment"></span><h1>__main__ — Top-level code environment</h1> <p>In Python, the special name <code>__main__</code> is used for two important constructs:</p> <ol class="arabic simple"> <li>the name of the top-level environment of the program, which can be checked using the <code>__name__ == '__main__'</code> expression; and</li> <li>the <code>__main__.py</code> file in Python packages.</li> </ol> <p>Both of these mechanisms are related to Python modules; how users interact with them and how they interact with each other. They are explained in detail below. If you’re new to Python modules, see the tutorial section <a class="reference internal" href="../tutorial/modules#tut-modules"><span class="std std-ref">Modules</span></a> for an introduction.</p> <section id="name-main"> <span id="name-equals-main"></span><h2><code>__name__ == '__main__'</code></h2> <p>When a Python module or package is imported, <code>__name__</code> is set to the module’s name. Usually, this is the name of the Python file itself without the <code>.py</code> extension:</p> <pre data-language="python">&gt;&gt;&gt; import configparser
+&gt;&gt;&gt; configparser.__name__
+'configparser'
+</pre> <p>If the file is part of a package, <code>__name__</code> will also include the parent package’s path:</p> <pre data-language="python">&gt;&gt;&gt; from concurrent.futures import process
+&gt;&gt;&gt; process.__name__
+'concurrent.futures.process'
+</pre> <p>However, if the module is executed in the top-level code environment, its <code>__name__</code> is set to the string <code>'__main__'</code>.</p> <section id="what-is-the-top-level-code-environment"> <h3>What is the “top-level code environment”?</h3> <p><code>__main__</code> is the name of the environment where top-level code is run. “Top-level code” is the first user-specified Python module that starts running. It’s “top-level” because it imports all other modules that the program needs. Sometimes “top-level code” is called an <em>entry point</em> to the application.</p> <p>The top-level code environment can be:</p> <ul> <li>
+<p>the scope of an interactive prompt:</p> <pre data-language="python">&gt;&gt;&gt; __name__
+'__main__'
+</pre> </li> <li>
+<p>the Python module passed to the Python interpreter as a file argument:</p> <pre data-language="shell">$ python helloworld.py
+Hello, world!
+</pre> </li> <li>
+<p>the Python module or package passed to the Python interpreter with the <a class="reference internal" href="../using/cmdline#cmdoption-m"><code>-m</code></a> argument:</p> <pre data-language="shell">$ python -m tarfile
+usage: tarfile.py [-h] [-v] (...)
+</pre> </li> <li>
+<p>Python code read by the Python interpreter from standard input:</p> <pre data-language="shell">$ echo "import this" | python
+The Zen of Python, by Tim Peters
+
+Beautiful is better than ugly.
+Explicit is better than implicit.
+...
+</pre> </li> <li>
+<p>Python code passed to the Python interpreter with the <a class="reference internal" href="../using/cmdline#cmdoption-c"><code>-c</code></a> argument:</p> <pre data-language="shell">$ python -c "import this"
+The Zen of Python, by Tim Peters
+
+Beautiful is better than ugly.
+Explicit is better than implicit.
+...
+</pre> </li> </ul> <p>In each of these situations, the top-level module’s <code>__name__</code> is set to <code>'__main__'</code>.</p> <p>As a result, a module can discover whether or not it is running in the top-level environment by checking its own <code>__name__</code>, which allows a common idiom for conditionally executing code when the module is not initialized from an import statement:</p> <pre data-language="python">if __name__ == '__main__':
+ # Execute when the module is not initialized from an import statement.
+ ...
+</pre> <div class="admonition seealso"> <p class="admonition-title">See also</p> <p>For a more detailed look at how <code>__name__</code> is set in all situations, see the tutorial section <a class="reference internal" href="../tutorial/modules#tut-modules"><span class="std std-ref">Modules</span></a>.</p> </div> </section> <section id="idiomatic-usage"> <h3>Idiomatic Usage</h3> <p>Some modules contain code that is intended for script use only, like parsing command-line arguments or fetching data from standard input. If a module like this was imported from a different module, for example to unit test it, the script code would unintentionally execute as well.</p> <p>This is where using the <code>if __name__ == '__main__'</code> code block comes in handy. Code within this block won’t run unless the module is executed in the top-level environment.</p> <p>Putting as few statements as possible in the block below <code>if __name__ ==
+'__main__'</code> can improve code clarity and correctness. Most often, a function named <code>main</code> encapsulates the program’s primary behavior:</p> <pre data-language="python"># echo.py
+
+import shlex
+import sys
+
+def echo(phrase: str) -&gt; None:
+ """A dummy wrapper around print."""
+ # for demonstration purposes, you can imagine that there is some
+ # valuable and reusable logic inside this function
+ print(phrase)
+
+def main() -&gt; int:
+ """Echo the input arguments to standard output"""
+ phrase = shlex.join(sys.argv)
+ echo(phrase)
+ return 0
+
+if __name__ == '__main__':
+ sys.exit(main()) # next section explains the use of sys.exit
+</pre> <p>Note that if the module didn’t encapsulate code inside the <code>main</code> function but instead put it directly within the <code>if __name__ == '__main__'</code> block, the <code>phrase</code> variable would be global to the entire module. This is error-prone as other functions within the module could be unintentionally using the global variable instead of a local name. A <code>main</code> function solves this problem.</p> <p>Using a <code>main</code> function has the added benefit of the <code>echo</code> function itself being isolated and importable elsewhere. When <code>echo.py</code> is imported, the <code>echo</code> and <code>main</code> functions will be defined, but neither of them will be called, because <code>__name__ != '__main__'</code>.</p> </section> <section id="packaging-considerations"> <h3>Packaging Considerations</h3> <p><code>main</code> functions are often used to create command-line tools by specifying them as entry points for console scripts. When this is done, <a class="reference external" href="https://pip.pypa.io/">pip</a> inserts the function call into a template script, where the return value of <code>main</code> is passed into <a class="reference internal" href="sys#sys.exit" title="sys.exit"><code>sys.exit()</code></a>. For example:</p> <pre data-language="python">sys.exit(main())
+</pre> <p>Since the call to <code>main</code> is wrapped in <a class="reference internal" href="sys#sys.exit" title="sys.exit"><code>sys.exit()</code></a>, the expectation is that your function will return some value acceptable as an input to <a class="reference internal" href="sys#sys.exit" title="sys.exit"><code>sys.exit()</code></a>; typically, an integer or <code>None</code> (which is implicitly returned if your function does not have a return statement).</p> <p>By proactively following this convention ourselves, our module will have the same behavior when run directly (i.e. <code>python echo.py</code>) as it will have if we later package it as a console script entry-point in a pip-installable package.</p> <p>In particular, be careful about returning strings from your <code>main</code> function. <a class="reference internal" href="sys#sys.exit" title="sys.exit"><code>sys.exit()</code></a> will interpret a string argument as a failure message, so your program will have an exit code of <code>1</code>, indicating failure, and the string will be written to <a class="reference internal" href="sys#sys.stderr" title="sys.stderr"><code>sys.stderr</code></a>. The <code>echo.py</code> example from earlier exemplifies using the <code>sys.exit(main())</code> convention.</p> <div class="admonition seealso"> <p class="admonition-title">See also</p> <p><a class="reference external" href="https://packaging.python.org/">Python Packaging User Guide</a> contains a collection of tutorials and references on how to distribute and install Python packages with modern tools.</p> </div> </section> </section> <section id="main-py-in-python-packages"> <h2>
+<code>__main__.py</code> in Python Packages</h2> <p>If you are not familiar with Python packages, see section <a class="reference internal" href="../tutorial/modules#tut-packages"><span class="std std-ref">Packages</span></a> of the tutorial. Most commonly, the <code>__main__.py</code> file is used to provide a command-line interface for a package. Consider the following hypothetical package, “bandclass”:</p> <pre data-language="text">bandclass
+ ├── __init__.py
+ ├── __main__.py
+ └── student.py
+</pre> <p><code>__main__.py</code> will be executed when the package itself is invoked directly from the command line using the <a class="reference internal" href="../using/cmdline#cmdoption-m"><code>-m</code></a> flag. For example:</p> <pre data-language="shell">$ python -m bandclass
+</pre> <p>This command will cause <code>__main__.py</code> to run. How you utilize this mechanism will depend on the nature of the package you are writing, but in this hypothetical case, it might make sense to allow the teacher to search for students:</p> <pre data-language="python"># bandclass/__main__.py
+
+import sys
+from .student import search_students
+
+student_name = sys.argv[1] if len(sys.argv) &gt;= 2 else ''
+print(f'Found student: {search_students(student_name)}')
+</pre> <p>Note that <code>from .student import search_students</code> is an example of a relative import. This import style can be used when referencing modules within a package. For more details, see <a class="reference internal" href="../tutorial/modules#intra-package-references"><span class="std std-ref">Intra-package References</span></a> in the <a class="reference internal" href="../tutorial/modules#tut-modules"><span class="std std-ref">Modules</span></a> section of the tutorial.</p> <section id="id1"> <h3>Idiomatic Usage</h3> <p>The content of <code>__main__.py</code> typically isn’t fenced with an <code>if __name__ == '__main__'</code> block. Instead, those files are kept short and import functions to execute from other modules. Those other modules can then be easily unit-tested and are properly reusable.</p> <p>If used, an <code>if __name__ == '__main__'</code> block will still work as expected for a <code>__main__.py</code> file within a package, because its <code>__name__</code> attribute will include the package’s path if imported:</p> <pre data-language="python">&gt;&gt;&gt; import asyncio.__main__
+&gt;&gt;&gt; asyncio.__main__.__name__
+'asyncio.__main__'
+</pre> <p>This won’t work for <code>__main__.py</code> files in the root directory of a .zip file though. Hence, for consistency, minimal <code>__main__.py</code> like the <a class="reference internal" href="venv#module-venv" title="venv: Creation of virtual environments."><code>venv</code></a> one mentioned below are preferred.</p> <div class="admonition seealso"> <p class="admonition-title">See also</p> <p>See <a class="reference internal" href="venv#module-venv" title="venv: Creation of virtual environments."><code>venv</code></a> for an example of a package with a minimal <code>__main__.py</code> in the standard library. It doesn’t contain a <code>if __name__ == '__main__'</code> block. You can invoke it with <code>python -m venv [directory]</code>.</p> <p>See <a class="reference internal" href="runpy#module-runpy" title="runpy: Locate and run Python modules without importing them first."><code>runpy</code></a> for more details on the <a class="reference internal" href="../using/cmdline#cmdoption-m"><code>-m</code></a> flag to the interpreter executable.</p> <p>See <a class="reference internal" href="zipapp#module-zipapp" title="zipapp: Manage executable Python zip archives"><code>zipapp</code></a> for how to run applications packaged as <em>.zip</em> files. In this case Python looks for a <code>__main__.py</code> file in the root directory of the archive.</p> </div> </section> </section> <section id="import-main"> <h2><code>import __main__</code></h2> <p>Regardless of which module a Python program was started with, other modules running within that same program can import the top-level environment’s scope (<a class="reference internal" href="../glossary#term-namespace"><span class="xref std std-term">namespace</span></a>) by importing the <code>__main__</code> module. This doesn’t import a <code>__main__.py</code> file but rather whichever module that received the special name <code>'__main__'</code>.</p> <p>Here is an example module that consumes the <code>__main__</code> namespace:</p> <pre data-language="python"># namely.py
+
+import __main__
+
+def did_user_define_their_name():
+ return 'my_name' in dir(__main__)
+
+def print_user_name():
+ if not did_user_define_their_name():
+ raise ValueError('Define the variable `my_name`!')
+
+ if '__file__' in dir(__main__):
+ print(__main__.my_name, "found in file", __main__.__file__)
+ else:
+ print(__main__.my_name)
+</pre> <p>Example usage of this module could be as follows:</p> <pre data-language="python"># start.py
+
+import sys
+
+from namely import print_user_name
+
+# my_name = "Dinsdale"
+
+def main():
+ try:
+ print_user_name()
+ except ValueError as ve:
+ return str(ve)
+
+if __name__ == "__main__":
+ sys.exit(main())
+</pre> <p>Now, if we started our program, the result would look like this:</p> <pre data-language="shell">$ python start.py
+Define the variable `my_name`!
+</pre> <p>The exit code of the program would be 1, indicating an error. Uncommenting the line with <code>my_name = "Dinsdale"</code> fixes the program and now it exits with status code 0, indicating success:</p> <pre data-language="shell">$ python start.py
+Dinsdale found in file /path/to/start.py
+</pre> <p>Note that importing <code>__main__</code> doesn’t cause any issues with unintentionally running top-level code meant for script use which is put in the <code>if __name__ == "__main__"</code> block of the <code>start</code> module. Why does this work?</p> <p>Python inserts an empty <code>__main__</code> module in <a class="reference internal" href="sys#sys.modules" title="sys.modules"><code>sys.modules</code></a> at interpreter startup, and populates it by running top-level code. In our example this is the <code>start</code> module which runs line by line and imports <code>namely</code>. In turn, <code>namely</code> imports <code>__main__</code> (which is really <code>start</code>). That’s an import cycle! Fortunately, since the partially populated <code>__main__</code> module is present in <a class="reference internal" href="sys#sys.modules" title="sys.modules"><code>sys.modules</code></a>, Python passes that to <code>namely</code>. See <a class="reference internal" href="../reference/import#import-dunder-main"><span class="std std-ref">Special considerations for __main__</span></a> in the import system’s reference for details on how this works.</p> <p>The Python REPL is another example of a “top-level environment”, so anything defined in the REPL becomes part of the <code>__main__</code> scope:</p> <pre data-language="python">&gt;&gt;&gt; import namely
+&gt;&gt;&gt; namely.did_user_define_their_name()
+False
+&gt;&gt;&gt; namely.print_user_name()
+Traceback (most recent call last):
+...
+ValueError: Define the variable `my_name`!
+&gt;&gt;&gt; my_name = 'Jabberwocky'
+&gt;&gt;&gt; namely.did_user_define_their_name()
+True
+&gt;&gt;&gt; namely.print_user_name()
+Jabberwocky
+</pre> <p>Note that in this case the <code>__main__</code> scope doesn’t contain a <code>__file__</code> attribute as it’s interactive.</p> <p>The <code>__main__</code> scope is used in the implementation of <a class="reference internal" href="pdb#module-pdb" title="pdb: The Python debugger for interactive interpreters."><code>pdb</code></a> and <a class="reference internal" href="rlcompleter#module-rlcompleter" title="rlcompleter: Python identifier completion, suitable for the GNU readline library."><code>rlcompleter</code></a>.</p> </section> <div class="_attribution">
+ <p class="_attribution-p">
+ &copy; 2001&ndash;2023 Python Software Foundation<br>Licensed under the PSF License.<br>
+ <a href="https://docs.python.org/3.12/library/__main__.html" class="_attribution-link">https://docs.python.org/3.12/library/__main__.html</a>
+ </p>
+</div>