Python Tutorials - Herong's Tutorial Examples - v2.14, by Herong Yang
What Is the __main__ Module
This section provides a quick introduction on the __main__ module, which is a special 'module' object created by the Python system as the first module to start executing user specified Python code.
What Is the __main__ Module? The __main__ module is a special "module" object created by the Python system as the first module to start executing user specified Python code. The __main__ module is referred as the entry point of the Python application execution.
The __main__ module can be identified by module's "__name__" attribute: __name__ == '__main__'.
If you start a Python interactive shell, you will be given an empty __main__ module:
herong$ python # name of the current module >>> __name__ '__main__' # built-in attributes in the module >>> dir() ['__annotations__', '__builtins__', '__doc__', '__loader__', '__name__', '__package__', '__spec__'] >>> __annotations__ {} >>> __builtins__ <module 'builtins' (built-in)> >>> __doc__ >>> __loader__ <class '_frozen_importlib.BuiltinImporter'> >>> __name__ '__main__' >>> __package__ >>> __spec__
If you start Python with a given Python code file, the code file will be loaded as the __main__ module and executed immediately.
To test this behavior, I cloned another copy of module_test.py to print the current module __name__ value:
# module_test_3.py #- Copyright 2011 (c) HerongYang.com. All Rights Reserved. # print("Hi there! - from 'module_test' module") version = "1.00 - from 'version' attribute" def help(): print("How can I help you? - from 'help()' function") class first: print("Welcome on board! - from 'first' class") count = 0 def rise(self): first.count += 1 print(f"count = {first.count} - from 'rise()' method") print(f"Current module __name__ = {__name__} - from module_test.py")
Now start Python with module_test_3.py:
herong$ python module_test_3.py Hi there! - from 'module_test' module Welcome on board! - from 'first' class current module __name__ = __main__ - from module_test.py
Since a Python code file can also be executed and imported as a module file, we can design it to have different execution branches for two execution mode:
1. Executed as a standalone application - The code file is loaded by the "python" command line program, and executed under the __main__ module. If the code file is designed to offer functions and classes to other applications, you can use this mode to test those functions and classes.
2. Executed as a supporting module to other applications - The code file is loaded by an "import" statement in other applications, and executed under the module named after its file name. If the code file is designed only as a standalone application, you can use this mode to raise an error or terminate the execution.
The code file below shows you how to write different branches for different execution mode.
# module_test_4.py #- Copyright 2011 (c) HerongYang.com. All Rights Reserved. version = "1.00 - from 'version' attribute" def help(): print("How can I help you? - from 'help()' function") class first: print("Welcome on board! - from 'first' class") count = 0 def rise(self): first.count += 1 print(f"count = {first.count} - from 'rise()' method") print(f"__name__ = {__name__}") # standalone application code branch if __name__ == '__main__': print(f"Running in standalone mode: do real work or test only") # supporting module code branch elif __name__ == 'module_test_4': print(f"Running in import mode: be quiet or stop it") else: print(f"Should never reach here: stop it")
If you run the above code file with the "python" command, you will get:
herong$ python module_test_4.py Welcome on board! - from 'first' class __name__ = __main__ Running in standalone mode: do real work or test only
If you import the above code file in another Python application, you will get:
herong$ python >>> import module_test_4 as m Welcome on board! - from 'first' class __name__ = module_test_4 Running in import mode: be quiet or stop it
Table of Contents
Variables, Operations and Expressions
Function Statement and Function Call
Iterators, Generators and List Comprehensions
"import module" - Two-Step Process
sys.modules - Listing Loaded Modules
importlib.reload(module) - Reloading Module
"from module import member" Statement
"from module import *" Statement
__pycache__/module.version.pyc Files
Packages and Package Directories
"pathlib" - Object-Oriented Filesystem Paths
"pip" - Package Installer for Python
SciPy.org - Python Libraries for Science
pandas - Data Analysis and Manipulation
Anaconda - Python Environment Manager