Computer Science - Chapter 9 Modules
__name__
- A global string variable automatically added to every module that contains the name of the module - Ex: my_funcs.__name__ would have the value "my_funcs", and google_search.__name__ would have the value "google_search"
From
- A programmer can specify names to import from a module by using the from keyword in an import statement - Ex: from module_name import name1, name2, ...
__main__
- The value of __name__ for the executing script is always set to "__main__" to differentiate the script from imported modules - The comparison if __name__ == "__main__": can be used to determine if the module is being run as a script or is being imported. If true, the file is being executed as a script and the branch is taken If false, the file was imported and thus __name__ is equal to the module name
Module
A file contained Python code that can be imported and used by scripts, other modules, or the interactive interpreter
Built-In
A module that comes pre-installed with Python; examples of built-in modules include sys, time, and math
Script
File passed to the interpreter as input that contains Python code
Dependency
The benefit of placing import statements at the top is that a reader of the program can quickly identify the modules required for the program to run. A module being required by another program is often called a dependency.
Import
To import a module means to execute the code contained by the module, and make the definitions within that module available for use by the importing program.