Log In

Don't have an account? Sign up now

Lost Password?

Sign Up

Prev Next

MODULE 6: Python Modules & Packages

Python allows you to organize large programs into reusable, manageable components using modules and packages.

1. What are Modules?

Brief

A module is a Python file (.py) that contains variables, functions, and classes which can be reused in other programs.

Why Modules Are Important

  • Improves code reusability
  • Makes code more organized
  • Easier debugging and maintenance
  • Enables teamwork in large projects

Example

# math_operations.py (module)
def add(a, b):
    return a + b

def subtract(a, b):
    return a - b

2. Importing Modules

Brief

Importing allows you to use code written in another module.


Ways to Import Modules

1. import module_name

import math
print(math.sqrt(16))

2. import module as alias

import math as m
print(m.pi)

3. from module import function

from math import sqrt
print(sqrt(25))

**4. from module import ***

(Not recommended – pollutes namespace)

from math import *
print(pi)

3. Built-in Modules

Brief

Python provides many ready-to-use built-in modules.


Common Built-in Modules & Usage

math

import math
print(math.factorial(5))
print(math.ceil(4.3))

random

import random
print(random.randint(1, 10))

datetime

from datetime import datetime
print(datetime.now())

os

import os
print(os.getcwd())

sys

import sys
print(sys.version)

4. Creating Custom Modules

Brief

You can create your own module by simply creating a .py file.


Step 1: Create Module

# calculator.py
def multiply(a, b):
    return a * b

def divide(a, b):
    return a / b

Step 2: Use Module

import calculator

print(calculator.multiply(5, 4))

Using Alias

import calculator as calc
print(calc.divide(10, 2))

5. Python Packages

Brief

A package is a collection of related modules organized in folders.


Package Structure

mypackage/
│
├── __init__.py
├── math_utils.py
├── string_utils.py

Example Module

# math_utils.py
def square(n):
    return n * n

Import from Package

from mypackage.math_utils import square
print(square(6))

Why Packages Are Used

  • Organize large applications
  • Avoid name conflicts
  • Improve scalability

6. pip & Virtual Environments


6.1 pip (Python Package Installer)

Brief

pip is used to install third-party libraries from PyPI.

Common Commands

pip install numpy
pip install pandas
pip uninstall numpy
pip list

6.2 Virtual Environments

Brief

A virtual environment creates an isolated Python environment for each project.

Why Virtual Environments Matter

  • Prevent dependency conflicts
  • Maintain clean project setup
  • Used in real-world deployments

Create Virtual Environment

python -m venv venv

Activate Virtual Environment

Windows

venv\Scripts\activate

Mac/Linux

source venv/bin/activate

7. requirements.txt

Brief

requirements.txt lists all project dependencies.


Generate requirements.txt

pip freeze > requirements.txt

Sample requirements.txt

numpy==1.26.2
pandas==2.1.0
flask==3.0.0

Install from requirements.txt

pip install -r requirements.txt

Leave a Comment

    🚀 Join Common Jobs Pro — Referrals & Profile Visibility Join Now ×
    🔥