DSPython Logo DSPython

Modules & Packages

Organize your code, use the Standard Library, and master the art of importing.

Python Intermediate Intermediate 45 min

πŸ“¦ 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:

import module_name
# OR
import module_name as alias
# OR
from module_name import specific_function

πŸ’» Example: Using Built-in Math

# Method 1: Import the whole module
import math
print(math.sqrt(16)) # Output: 4.0

# Method 2: Import with Alias (Short name)
import math as m
print(m.pi) # Output: 3.14159...

# Method 3: Import specific function (No dot needed)
from math import ceil
print(ceil(4.2)) # Output: 5

πŸ”‹ 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:

math

Advanced math functions (sin, cos, log, sqrt).

random

Generate random numbers or pick random items.

datetime

Work with dates, times, and durations.

os

Interact with the Operating System (files, paths).

πŸ’» Example: Random & Datetime

import random
import datetime

# Pick a random winner
names = ["Vinay", "Ravi", "Sita"]
print(random.choice(names))

# Get current time
now = datetime.datetime.now()
print(now)

πŸ“ 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):

# Check if pip is installed
$ pip --version

# Install a package (e.g., pandas)
$ pip install pandas

# List installed packages
$ pip list

πŸ“š Module Summary

  • Module: A single .py file 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.

πŸ€–
DSPython AI Assistant βœ–
πŸ‘‹ Hi! I’m your AI assistant. Paste your code here, I will find bugs for you.