__main__ — Top-level code environment — Python 3.16.0a0 documentation

docs.python.org

Tin mới

builtins — Built-in objects

warnings — Warning control

Python Module Index

3.16.0a0 Documentation

The Python Standard Library

Python Runtime Services

__name__ == '__main__'¶

In Python, the special name __main__ is used for two important constructs:

Packaging Considerations¶

main functions are often used to create command-line tools by specifying them as entry points for console scripts. When this is done, pip inserts the function call into a template script, where the return value of main i

__main__.py in Python Packages¶

If you are not familiar with Python packages, see section Packages of the tutorial. Most commonly, the __main__.py file is used to provide a command-line interface for a package. Consider the following hypothetical packa

venv: Creation of virtual environments.

See venv for an example of a package with a minimal __main__.py in the standard library. It doesn’t contain a if __name__ == '__main__' block. You can invoke it with python -m venv [directory].

runpy: Locate and run Python modules without importing them first.

See venv for an example of a package with a minimal __main__.py in the standard library. It doesn’t contain a if __name__ == '__main__' block. You can invoke it with python -m venv [directory].

zipapp: Manage executable Python zip archives

See venv for an example of a package with a minimal __main__.py in the standard library. It doesn’t contain a if __name__ == '__main__' block. You can invoke it with python -m venv [directory].

Special considerations for __main__

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 (namespace) by importing the __main__ module. This doesn’t import

pdb: The Python debugger for interactive interpreters.

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 (namespace) by importing the __main__ module. This doesn’t import

rlcompleter: Python identifier completion, suitable for the GNU readline library.

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 (namespace) by importing the __main__ module. This doesn’t import