Why should you use modules in Python?
Because definitions (functions, variables) are lost when the interpreter exits. Modules let you preserve and reuse code across scripts.
What is a module?
A .py file containing Python code (functions, variables, classes). It can be imported into scripts or the interactive interpreter.
What is the purpose of \_\_name\_\_ in a module?
It’s a global variable set to the module name, or '\_\_main\_\_' when the file is executed as a script.
How do you import a module and use a function from it?
Use import module_name, then call the function with module_name.function().
How do you import a specific function from a module?
Use from module import function. Example: from fibo import fib.
What does from module import * do and why is it discouraged?
It imports all non-underscore-prefixed names. It’s discouraged because it can overwrite existing names and pollutes the namespace.
How can you assign an alias to an imported module?
Use import module as alias. Example: import fibo as fib.
How do you rename a function on import?
Use from module import function as new_name. Example: from fibo import fib as fibonacci.
How do you write a module to work both as a script and as an importable module?
Wrap script logic in if \_\_name\_\_ == '\_\_main\_\_':. Example:
```python
if __name__ == ‘__main__’:
import sys
fib(int(sys.argv[1]))
~~~
In what order does Python search for modules?
What is sys.path and how can it be used?
sys.path is a list of directories Python searches to find modules. It can be modified at runtime using list operations.
Where are compiled Python files stored?
In the \_\_pycache\_\_ directory, with names like module.cpython-XY.pyc.
How does Python decide to recompile a module?
Python recompiles if the source file is newer than the .pyc file or if the .pyc is missing.
What do the -O and -OO flags do?
-O removes assert statements. -OO removes assert and docstrings, creating optimized .pyc files.
What does dir() do?
Lists names defined in a module or the current local scope.
How do you list built-in names in Python?
Import the builtins module and run dir(builtins).
What is a package in Python?
A directory with an \_\_init\_\_.py file, allowing a hierarchical structure using dotted module names.
How do you import submodules from a package?
Use syntax like import package.subpackage.module or from package.subpackage import module.
What is \_\_all\_\_ used for in a package’s \_\_init\_\_.py?
Defines which modules to import when using from package import *. Example:
```python
__all__ = [‘echo’, ‘surround’, ‘reverse’]
~~~
What happens if \_\_all\_\_ is not defined?
Only names defined in \_\_init\_\_.py or already imported submodules will be available.
What are relative imports and how are they written?
They use dots to refer to sibling/parent modules. Examples:
```python
from . import echo
from .. import formats
~~~
Can the main script use relative imports?
No. Relative imports only work within packages, not in the main script.
What is \_\_path\_\_ used for in packages?
It’s a list of directories Python uses to search for submodules in a package. It can be modified dynamically.