• Home
  • Raw
  • Download

Lines Matching +full:python3 +full:- +full:pip

1 :mod:`__main__` --- Top-level code environment
5 :synopsis: The environment where top-level code is run. Covers command-line
6 interfaces, import-time behavior, and ``__name__ == '__main__'``.
8 --------------
12 1. the name of the top-level environment of the program, which can be
19 :ref:`tut-modules` for an introduction.
25 ---------------------------
42 However, if the module is executed in the top-level code environment,
45 What is the "top-level code environment"?
48 ``__main__`` is the name of the environment where top-level code is run.
49 "Top-level code" is the first user-specified Python module that starts running.
50 It's "top-level" because it imports all other modules that the program needs.
51 Sometimes "top-level code" is called an *entry point* to the application.
53 The top-level code environment can be:
62 .. code-block:: shell-session
64 $ python3 helloworld.py
68 :option:`-m` argument:
70 .. code-block:: shell-session
72 $ python3 -m tarfile
73 usage: tarfile.py [-h] [-v] (...)
77 .. code-block:: shell-session
79 $ echo "import this" | python3
86 * Python code passed to the Python interpreter with the :option:`-c` argument:
88 .. code-block:: shell-session
90 $ python3 -c "import this"
97 In each of these situations, the top-level module's ``__name__`` is set to
101 top-level environment by checking its own ``__name__``, which allows a common
112 the tutorial section :ref:`tut-modules`.
119 command-line arguments or fetching data from standard input. If a module
125 top-level environment.
136 def echo(phrase: str) -> None:
142 def main() -> int:
154 error-prone as other functions within the module could be unintentionally using
167 ``main`` functions are often used to create command-line tools by specifying
169 `pip <https://pip.pypa.io/>`_ inserts the function call into a template script,
181 same behavior when run directly (i.e. ``python3 echo.py``) as it will have if
182 we later package it as a console script entry-point in a pip-installable
199 ----------------------------------
201 If you are not familiar with Python packages, see section :ref:`tut-packages`
203 a command-line interface for a package. Consider the following hypothetical
206 .. code-block:: text
214 directly from the command line using the :option:`-m` flag. For example:
216 .. code-block:: shell-session
218 $ python3 -m bandclass
235 package. For more details, see :ref:`intra-package-references` in the
236 :ref:`tut-modules` section of the tutorial.
244 easily unit-tested and are properly reusable.
262 block. You can invoke it with ``python -m venv [directory]``.
264 See :mod:`runpy` for more details on the :option:`-m` flag to the
274 -------------------
277 running within that same program can import the top-level environment's scope
321 .. code-block:: shell-session
323 $ python3 start.py
330 .. code-block:: shell-session
332 $ python3 start.py
336 running top-level code meant for script use which is put in the
340 interpreter startup, and populates it by running top-level code. In our example
345 See :ref:`Special considerations for __main__ <import-dunder-main>` in the
348 The Python REPL is another example of a "top-level environment", so anything