Modules & Packages
Organize your code, use the Standard Library, and master the art of importing.
π¦ Introduction: The "Supermarket" Analogy
Imagine if a Supermarket kept all its items in one giant pile in the middle of the floor. Finding a toothbrush or a tomato would be impossible!
Instead, they organize items into Aisles (Vegetables, Electronics, Toiletries).
In Python:
1. Code is the item (Functions, Variables).
2. Module is the Aisle (A single file containing related code).
3. Package is the Department (A folder containing multiple modules).
Modules allow us to break large programs into small, manageable files. This makes code easier to read, maintain, and reuse.
π Topic 1: What is a Module?
A module is simply a file ending in .py. It can contain functions, classes, and variables. To use the code from one module in another, we use the import statement.
π The Import Syntax:
# OR
import module_name as alias
# OR
from module_name import specific_function
π» Example: Using Built-in Math
π Topic 2: Standard Library ("Batteries Included")
Python is famous for having "Batteries Included". This means when you install Python, it comes with a massive library of useful modules so you don't have to write code from scratch.
π Popular Built-in Modules:
Advanced math functions (sin, cos, log, sqrt).
Generate random numbers or pick random items.
Work with dates, times, and durations.
Interact with the Operating System (files, paths).
π» Example: Random & Datetime
π Topic 3: Packages
A Package is essentially a directory (folder) that contains multiple modules. It creates a hierarchy (Tree structure) for your code.
To make Python treat a folder as a package, it typically needs a special file named __init__.py inside it (though this is optional in newer Python versions, it is still best practice).
π Directory Structure Example:
- π Game/ (This is a Package)
- π __init__.py
- π Sound/ (Sub-package)
- π __init__.py
- π play.py
- π pause.py
- π Graphics/ (Sub-package)
- π __init__.py
- π render.py
Importing from packages:
from Game.Sound import play
play.start_music()
π Topic 4: Pip & PyPI (The Global Store)
What if the module you want isn't in the Standard Library? (e.g., Data Science tools like pandas, Web tools like Django).
You download them from PyPI (Python Package Index). It is a repository of software for the Python programming language. To install them, we use a tool called pip.
π» Terminal Commands (Not Python Code):
π Module Summary
- Module: A single
.pyfile containing code. - Import: The keyword used to bring a module into your code.
- Standard Library: Built-in modules like
math,random,os. - Package: A folder containing modules (usually with
__init__.py). - Pip: The package installer for Python (used in terminal).
π€ Interview Q&A
Tap on the questions below to reveal the answers.
A Module is a single file (e.g., math.py) containing Python code. A Package is a directory (folder) that contains multiple modules and usually an __init__.py file.
This check ensures that a block of code only runs if the file is executed directly (e.g., python file.py). If the file is imported as a module into another script, that block of code is ignored.
This error means Python cannot find the module you are trying to import. Usually, you need to install it using pip install package_name, or ensure the file path is correct if it's your own module.
It is a special file that tells Python "Treat this directory as a package." It can also be used to initialize package-level variables or simplify imports for the user.