Docs

modules packages

06 - Modules & Packages

šŸ“Œ What You'll Learn

  • •What are modules and packages
  • •Importing modules
  • •Creating your own modules
  • •Creating packages
  • •Python Standard Library
  • •pip and virtual environments
  • •Best practices

šŸ“¦ What is a Module?

A module is simply a Python file (.py) containing code (functions, classes, variables) that can be reused in other programs.

Why Use Modules?

  • •Code organization - Split large programs into smaller files
  • •Code reuse - Import and use code in multiple programs
  • •Namespace management - Avoid naming conflicts
  • •Maintainability - Easier to update and fix bugs

šŸ“„ Importing Modules

Basic Import

# Import entire module
import math

print(math.pi)        # 3.141592653589793
print(math.sqrt(16))  # 4.0

# Import with alias
import math as m

print(m.pi)
print(m.sqrt(16))

Import Specific Items

# Import specific functions/variables
from math import pi, sqrt

print(pi)        # No need for math. prefix
print(sqrt(16))

# Import with alias
from math import sqrt as square_root

print(square_root(16))

Import All (Not Recommended)

# Import everything - avoid this!
from math import *

print(pi)
print(sqrt(16))

# Problems:
# - Pollutes namespace
# - Can overwrite existing names
# - Hard to track where things come from

šŸ”§ Creating Your Own Module

Step 1: Create a Module File

# mymath.py - Your custom module

"""
My custom math module.
Contains basic mathematical operations.
"""

PI = 3.14159

def add(a, b):
    """Return the sum of a and b."""
    return a + b

def subtract(a, b):
    """Return the difference of a and b."""
    return a - b

def multiply(a, b):
    """Return the product of a and b."""
    return a * b

def divide(a, b):
    """Return the division of a by b."""
    if b == 0:
        raise ValueError("Cannot divide by zero")
    return a / b

class Calculator:
    """A simple calculator class."""

    def __init__(self):
        self.result = 0

    def add(self, x):
        self.result += x
        return self

Step 2: Use Your Module

# main.py - Using your module

import mymath

print(mymath.PI)
print(mymath.add(3, 5))

# Or import specific items
from mymath import add, Calculator

print(add(10, 20))
calc = Calculator()

šŸ“ Creating Packages

A package is a directory containing multiple modules and a special __init__.py file.

Package Structure

mypackage/
│
ā”œā”€ā”€ __init__.py          # Makes it a package
ā”œā”€ā”€ module1.py           # Submodule 1
ā”œā”€ā”€ module2.py           # Submodule 2
│
└── subpackage/          # Nested package
    ā”œā”€ā”€ __init__.py
    └── module3.py

init.py File

# mypackage/__init__.py

# Package initialization code
print("Initializing mypackage")

# Control what's exported with __all__
__all__ = ['module1', 'module2']

# Import submodules for easier access
from .module1 import function1
from .module2 import function2

Using Your Package

# Import entire package
import mypackage

# Import specific module
from mypackage import module1

# Import specific function
from mypackage.module1 import function1

# Import from subpackage
from mypackage.subpackage import module3

šŸ“š Python Standard Library

Python comes with a rich standard library. Here are the most useful modules:

os - Operating System Interface

import os

# Current working directory
print(os.getcwd())

# List directory contents
print(os.listdir('.'))

# Create directory
os.makedirs('new_folder', exist_ok=True)

# File path operations
print(os.path.exists('file.txt'))
print(os.path.join('folder', 'file.txt'))
print(os.path.splitext('file.txt'))  # ('file', '.txt')

# Environment variables
print(os.environ.get('HOME'))

sys - System-Specific Parameters

import sys

# Python version
print(sys.version)

# Command line arguments
print(sys.argv)

# Module search path
print(sys.path)

# Exit program
# sys.exit(0)

datetime - Date and Time

from datetime import datetime, date, timedelta

# Current date and time
now = datetime.now()
print(now)

# Format dates
print(now.strftime("%Y-%m-%d %H:%M:%S"))

# Parse dates
parsed = datetime.strptime("2024-01-15", "%Y-%m-%d")

# Date arithmetic
tomorrow = date.today() + timedelta(days=1)

random - Random Numbers

import random

# Random float [0.0, 1.0)
print(random.random())

# Random integer
print(random.randint(1, 100))

# Random choice
print(random.choice(['apple', 'banana', 'cherry']))

# Shuffle list
items = [1, 2, 3, 4, 5]
random.shuffle(items)

# Sample without replacement
print(random.sample(range(100), 5))

json - JSON Handling

import json

# Python to JSON
data = {"name": "Alice", "age": 25}
json_str = json.dumps(data, indent=2)

# JSON to Python
parsed = json.loads(json_str)

# File operations
with open('data.json', 'w') as f:
    json.dump(data, f)

with open('data.json', 'r') as f:
    loaded = json.load(f)

collections - Specialized Containers

from collections import Counter, defaultdict, namedtuple, deque

# Counter - count occurrences
counter = Counter(['a', 'b', 'a', 'c', 'a'])
print(counter.most_common(2))

# defaultdict - dict with default values
dd = defaultdict(list)
dd['key'].append(1)

# namedtuple - tuple with named fields
Point = namedtuple('Point', ['x', 'y'])
p = Point(3, 4)
print(p.x, p.y)

# deque - double-ended queue
dq = deque([1, 2, 3])
dq.appendleft(0)
dq.append(4)

pathlib - Modern Path Handling

from pathlib import Path

# Create path object
p = Path('/home/user/documents')

# Path operations
print(p.exists())
print(p.is_dir())
print(p.parent)
print(p.name)

# List files
for file in Path('.').glob('*.py'):
    print(file)

# Read/write files
path = Path('file.txt')
path.write_text('Hello, World!')
content = path.read_text()

šŸ“¦ pip and Virtual Environments

pip - Package Installer

# Install package
pip install requests

# Install specific version
pip install requests==2.28.0

# Install from requirements.txt
pip install -r requirements.txt

# List installed packages
pip list

# Show package info
pip show requests

# Uninstall package
pip uninstall requests

# Export requirements
pip freeze > requirements.txt

Virtual Environments

# Create virtual environment
python -m venv myenv

# Activate (Linux/Mac)
source myenv/bin/activate

# Activate (Windows)
myenv\Scripts\activate

# Deactivate
deactivate

# Delete environment (just delete the folder)
rm -rf myenv

Why Use Virtual Environments?

# Without virtual environment:
Project A needs requests==2.0
Project B needs requests==3.0
CONFLICT!

# With virtual environments:
Project A venv → requests==2.0
Project B venv → requests==3.0
No conflict!

šŸ” Module Search Path

Python looks for modules in this order:

import sys

for path in sys.path:
    print(path)

# 1. Current directory
# 2. PYTHONPATH environment variable
# 3. Standard library directories
# 4. Site-packages (installed packages)

Adding to Path

import sys
sys.path.append('/path/to/my/modules')

# Or set PYTHONPATH environment variable
# export PYTHONPATH=/path/to/my/modules

šŸ›”ļø if name == "main"

This idiom allows a module to be both imported and run directly.

# mymodule.py

def main():
    print("Running as main program")

def helper_function():
    print("Helper function")

# This block runs only when script is executed directly
# Not when imported as a module
if __name__ == "__main__":
    main()
# When run directly:
# python mymodule.py
# Output: Running as main program

# When imported:
# import mymodule
# Nothing is printed automatically

šŸ“‹ Best Practices

  1. •Use explicit imports instead of from module import *
  2. •Organize imports at the top of the file
  3. •Group imports: standard library, third-party, local
  4. •Use virtual environments for each project
  5. •Document your modules with docstrings
  6. •Use __all__ to control what's exported
  7. •Keep modules focused - one purpose per module
# Good import organization
import os
import sys
from collections import defaultdict

import requests
import numpy as np

from mypackage import mymodule
from mypackage.utils import helper

šŸŽÆ Next Steps

After mastering modules, proceed to 07_file_handling to learn about reading and writing files!

Modules Packages - Python Tutorial | DeepML