Chapter 20: Modules and Packages
Complete Python lesson for very beginners. Technical words are explained in simple language, and every outline topic includes a practical example, expected output, steps, and practice.
Chapter Overview
This Python tutorial chapter covers Modules and Packages through 15 connected topics. Work through the examples in order, check the expected output, and complete the practice after each topic.
- 20.1 What Is a Module?
- 20.2 import
- 20.3 from ... import
- 20.4 Aliases
- 20.5 Standard Library
- Plus 10 additional Python topics in this chapter.
20.1 What Is a Module?
What Is a Module? is part of Modules and Packages. In simple language, it means a Python file that can provide reusable code.
It is one building block of modules and packages and helps you write clearer, more predictable Python programs. For a very beginner, focus first on what goes in, what Python does, and what comes out; details become easier after you run a small example.
Python / Practical Example
import math
print(math.sqrt(81))Expected Output
9.0Step-by-Step Explanation
- import makes a module available.
- Use module.name to access its contents.
- The standard-library math module provides sqrt().
20.2 import
import is part of Modules and Packages. In simple language, it means a Python idea used while learning modules and packages.
It is one building block of modules and packages and helps you write clearer, more predictable Python programs. For a very beginner, focus first on what goes in, what Python does, and what comes out; details become easier after you run a small example.
Python / Practical Example
import math
print(math.sqrt(81))Expected Output
9.0Step-by-Step Explanation
- import makes a module available.
- Use module.name to access its contents.
- The standard-library math module provides sqrt().
20.3 from ... import
from ... import is part of Modules and Packages. In simple language, it means a Python idea used while learning modules and packages.
It is one building block of modules and packages and helps you write clearer, more predictable Python programs. For a very beginner, focus first on what goes in, what Python does, and what comes out; details become easier after you run a small example.
Python / Practical Example
import math
print(math.sqrt(81))Expected Output
9.0Step-by-Step Explanation
- import makes a module available.
- Use module.name to access its contents.
- The standard-library math module provides sqrt().
20.4 Aliases
Aliases is part of Modules and Packages. In simple language, it means a Python idea used while learning modules and packages.
It is one building block of modules and packages and helps you write clearer, more predictable Python programs. For a very beginner, focus first on what goes in, what Python does, and what comes out; details become easier after you run a small example.
Python / Practical Example
import math
print(math.pi > 3)Expected Output
TrueStep-by-Step Explanation
- Read the example from top to bottom.
- Identify the value, object, or operation related to this topic.
- Change one small input and predict the result before running it.
20.5 Standard Library
Standard Library is part of Modules and Packages. In simple language, it means a Python idea used while learning modules and packages.
It is one building block of modules and packages and helps you write clearer, more predictable Python programs. For a very beginner, focus first on what goes in, what Python does, and what comes out; details become easier after you run a small example.
Python / Practical Example
import math
print(math.pi > 3)Expected Output
TrueStep-by-Step Explanation
- Read the example from top to bottom.
- Identify the value, object, or operation related to this topic.
- Change one small input and predict the result before running it.
20.6 Creating Modules
Creating Modules is part of Modules and Packages. In simple language, it means a Python file that can provide reusable code.
It is one building block of modules and packages and helps you write clearer, more predictable Python programs. For a very beginner, focus first on what goes in, what Python does, and what comes out; details become easier after you run a small example.
Python / Practical Example
import math
print(math.sqrt(81))Expected Output
9.0Step-by-Step Explanation
- import makes a module available.
- Use module.name to access its contents.
- The standard-library math module provides sqrt().
20.7 __name__
__name__ is part of Modules and Packages. In simple language, it means a Python idea used while learning modules and packages.
It is one building block of modules and packages and helps you write clearer, more predictable Python programs. For a very beginner, focus first on what goes in, what Python does, and what comes out; details become easier after you run a small example.
Python / Practical Example
def main():
print("program started")
if __name__ == "__main__":
main()Expected Output
program startedStep-by-Step Explanation
- Python sets __name__ for each module.
- A directly executed file receives "__main__".
- The guard lets reusable imports avoid running the program entry point automatically.
20.8 __main__
__main__ is part of Modules and Packages. In simple language, it means a Python idea used while learning modules and packages.
It is one building block of modules and packages and helps you write clearer, more predictable Python programs. For a very beginner, focus first on what goes in, what Python does, and what comes out; details become easier after you run a small example.
Python / Practical Example
def main():
print("program started")
if __name__ == "__main__":
main()Expected Output
program startedStep-by-Step Explanation
- Python sets __name__ for each module.
- A directly executed file receives "__main__".
- The guard lets reusable imports avoid running the program entry point automatically.
20.9 Packages
Packages is part of Modules and Packages. In simple language, it means a way to organize or distribute related Python code.
It is one building block of modules and packages and helps you write clearer, more predictable Python programs. For a very beginner, focus first on what goes in, what Python does, and what comes out; details become easier after you run a small example.
Python / Practical Example
import math
print(math.sqrt(81))Expected Output
9.0Step-by-Step Explanation
- import makes a module available.
- Use module.name to access its contents.
- The standard-library math module provides sqrt().
20.10 __init__.py
__init__.py is part of Modules and Packages. In simple language, it means a Python idea used while learning modules and packages.
It is one building block of modules and packages and helps you write clearer, more predictable Python programs. For a very beginner, focus first on what goes in, what Python does, and what comes out; details become easier after you run a small example.
Python / Practical Example
import math
print(math.pi > 3)Expected Output
TrueStep-by-Step Explanation
- Read the example from top to bottom.
- Identify the value, object, or operation related to this topic.
- Change one small input and predict the result before running it.
20.11 Absolute Imports
Absolute Imports is part of Modules and Packages. In simple language, it means a Python idea used while learning modules and packages.
It is one building block of modules and packages and helps you write clearer, more predictable Python programs. For a very beginner, focus first on what goes in, what Python does, and what comes out; details become easier after you run a small example.
Python / Practical Example
print(abs(-12))Expected Output
12Step-by-Step Explanation
- abs() returns distance from zero.
- A negative input becomes positive magnitude.
- The result is numeric.
20.12 Relative Imports
Relative Imports is part of Modules and Packages. In simple language, it means a Python idea used while learning modules and packages.
It is one building block of modules and packages and helps you write clearer, more predictable Python programs. For a very beginner, focus first on what goes in, what Python does, and what comes out; details become easier after you run a small example.
Python / Practical Example
import math
print(math.sqrt(81))Expected Output
9.0Step-by-Step Explanation
- import makes a module available.
- Use module.name to access its contents.
- The standard-library math module provides sqrt().
20.13 Module Search Path
Module Search Path is part of Modules and Packages. In simple language, it means a Python file that can provide reusable code.
It helps you prepare a reliable Python workspace so the same commands and files run in the environment you expect. For a very beginner, focus first on what goes in, what Python does, and what comes out; details become easier after you run a small example.
Python / Practical Example
import math
print(math.sqrt(81))Expected Output
9.0Step-by-Step Explanation
- import makes a module available.
- Use module.name to access its contents.
- The standard-library math module provides sqrt().
20.14 Organizing Packages
Organizing Packages is part of Modules and Packages. In simple language, it means a way to organize or distribute related Python code.
It is one building block of modules and packages and helps you write clearer, more predictable Python programs. For a very beginner, focus first on what goes in, what Python does, and what comes out; details become easier after you run a small example.
Python / Practical Example
import math
print(math.sqrt(81))Expected Output
9.0Step-by-Step Explanation
- import makes a module available.
- Use module.name to access its contents.
- The standard-library math module provides sqrt().
20.15 Reusable Code
Reusable Code is part of Modules and Packages. In simple language, it means a Python idea used while learning modules and packages.
It is one building block of modules and packages and helps you write clearer, more predictable Python programs. For a very beginner, focus first on what goes in, what Python does, and what comes out; details become easier after you run a small example.
Python / Practical Example
import math
print(math.pi > 3)Expected Output
TrueStep-by-Step Explanation
- Read the example from top to bottom.
- Identify the value, object, or operation related to this topic.
- Change one small input and predict the result before running it.
Common Beginner Mistakes
- Copying code without predicting what each line does.
- Ignoring the first useful error message or traceback location.
- Mixing tabs/spaces or changing indentation accidentally.
- Using data of the wrong type for an operation.
- Trying to learn many advanced variations before mastering one small working example.
Chapter Practice
- Choose three topics from this chapter and re-type their examples without copying and pasting.
- For each example, change one input and predict the output first.
- Explain five technical terms from this chapter in your own beginner-friendly words.
- Create one small program that combines at least two chapter topics.
- Keep notes about errors you made and what fixed them.
Mini Project / Challenge
Create a small Python exercise that combines at least three ideas from Modules and Packages. Start with a tiny working version, test it, then improve it one step at a time.
- Choose three topics from this chapter.
- Write or adapt a small Python example using those topics.
- Predict the output before running the code.
- Test at least one different input.
- Write two sentences explaining what the program does and what you learned.
20 Questions & Answers
1. What is the main goal of Chapter 20?
The goal is to understand modules and packages through small explanations, examples, and practice.
2. Should I memorize every command or method?
No. Understand the pattern, practice the common form, and learn how to read documentation when you need exact details.
3. Why are the examples small?
Small examples isolate one idea at a time, which makes errors easier to understand and fix.
4. What should I do when an example gives an error?
Read the last part of the traceback, check spelling and indentation, confirm the required package or file exists, and compare the input types with what the operation expects.
5. Why should I predict output before running code?
Prediction forces you to reason about the program instead of only copying it.
6. What should I remember about What Is a Module??
Remember its beginner meaning, the problem it helps solve, the shape of a small example, and one common situation where it is appropriate.
7. What should I remember about import?
Remember its beginner meaning, the problem it helps solve, the shape of a small example, and one common situation where it is appropriate.
8. What should I remember about from ... import?
Remember its beginner meaning, the problem it helps solve, the shape of a small example, and one common situation where it is appropriate.
9. What should I remember about Aliases?
Remember its beginner meaning, the problem it helps solve, the shape of a small example, and one common situation where it is appropriate.
10. What should I remember about Standard Library?
Remember its beginner meaning, the problem it helps solve, the shape of a small example, and one common situation where it is appropriate.
11. What should I remember about Creating Modules?
Remember its beginner meaning, the problem it helps solve, the shape of a small example, and one common situation where it is appropriate.
12. What should I remember about __name__?
Remember its beginner meaning, the problem it helps solve, the shape of a small example, and one common situation where it is appropriate.
13. What should I remember about __main__?
Remember its beginner meaning, the problem it helps solve, the shape of a small example, and one common situation where it is appropriate.
14. What should I remember about Packages?
Remember its beginner meaning, the problem it helps solve, the shape of a small example, and one common situation where it is appropriate.
15. What should I remember about __init__.py?
Remember its beginner meaning, the problem it helps solve, the shape of a small example, and one common situation where it is appropriate.
16. What should I remember about Absolute Imports?
Remember its beginner meaning, the problem it helps solve, the shape of a small example, and one common situation where it is appropriate.
17. What should I remember about Relative Imports?
Remember its beginner meaning, the problem it helps solve, the shape of a small example, and one common situation where it is appropriate.
18. What should I remember about Module Search Path?
Remember its beginner meaning, the problem it helps solve, the shape of a small example, and one common situation where it is appropriate.
19. What should I remember about Organizing Packages?
Remember its beginner meaning, the problem it helps solve, the shape of a small example, and one common situation where it is appropriate.
20. What should I remember about Reusable Code?
Remember its beginner meaning, the problem it helps solve, the shape of a small example, and one common situation where it is appropriate.