Chapter 7: Python Modules & Packages

Hands-on exercises to practice organizing code with modules and packages

Chapter 7 Concepts

This chapter covers how to organize Python code using modules and packages:

Module Creation Importing Modules Package Structure Standard Library Modules Module Aliases PIP Installations Module Discovery __init__.py Files
Exercise 1

Create Your First Module

Create a simple math utility module with basic functions.

# Create a file named math_utils.py
# Add functions for:
# - add(a, b): returns sum of a and b
# - subtract(a, b): returns a minus b
# - multiply(a, b): returns a times b
# - divide(a, b): returns a divided by b
  • Create a new file named math_utils.py
  • Add the four arithmetic functions
  • Test the module by importing it in another script
  • Call each function with sample values
  • Print the results
  • Exercise 2

    Different Import Methods

    Practice different ways to import and use modules.

    # Using the math_utils module from Exercise 1:
    # 1. Import the entire module
    # 2. Import specific functions
    # 3. Import with an alias
    # 4. Use each import method
  • Import the entire module: import math_utils
  • Import specific functions: from math_utils import add
  • Import with alias: import math_utils as mu
  • Call functions using each import method
  • Compare the different syntax approaches
  • Exercise 3

    Create a Package

    Create a Python package with multiple modules.

    my_package/
    ├── __init__.py
    ├── string_utils.py
    └── number_utils.py
  • Create a directory named my_package
  • Add an __init__.py file (can be empty)
  • Create string_utils.py with string manipulation functions
  • Create number_utils.py with number-related functions
  • Import and use the package from another script
  • Exercise 4

    Explore Standard Library Modules

    Use Python's built-in modules for common tasks.

    # Use these standard library modules:
    # - math: mathematical functions
    # - datetime: date and time operations
    # - random: generate random numbers
    # - os: operating system interfaces
  • Use math.sqrt() to calculate square roots
  • Use datetime to get current date and time
  • Use random to generate random numbers
  • Use os to get the current working directory
  • Print results from each module usage
  • Exercise 5

    Module Aliases and Selective Import

    Practice using aliases and importing specific elements.

    # Practice these import techniques:
    # 1. Import numpy with alias np
    # 2. From math import pi, sqrt
    # 3. From datetime import date as d
    # 4. Use each imported element
  • Import numpy as np (if installed)
  • Selectively import pi and sqrt from math
  • Import date from datetime with alias
  • Use each imported element in calculations
  • Note the differences in usage syntax
  • Hint

    If numpy is not installed, you can use pip to install it: pip install numpy

    Exercise 6

    Install and Use External Packages

    Use pip to install and work with external packages.

    # Install and use these popular packages:
    # - requests: HTTP requests
    # - pandas: data manipulation
    # - matplotlib: data visualization
    # (Install with: pip install package_name)
  • Install requests using pip
  • Make a simple HTTP GET request
  • Install pandas and create a simple DataFrame
  • Install matplotlib and create a simple plot
  • Note the installation process and usage
  • Hint

    You may need to use virtual environments for package management in larger projects.

    Exercise 7

    Explore Module Contents

    Use Python's built-in functions to explore modules.

    # Use these exploration techniques:
    # - dir(module): list all attributes
    # - help(module): show documentation
    # - module.__doc__: module documentation
    # - module.__file__: location of module
  • Import the math module
  • Use dir() to list all available functions
  • Use help() to read documentation for specific functions
  • Access the module's __doc__ attribute
  • Find where the module is located using __file__
  • Exercise 8

    Create an __init__.py File

    Customize package initialization with __init__.py.

    # In your package's __init__.py:
    # 1. Import key functions from modules
    # 2. Define __all__ to control exports
    # 3. Add package-level documentation
    # 4. Initialize package-level variables
  • Open the __init__.py file in your package
  • Import important functions from your modules
  • Define __all__ to specify what gets imported with *
  • Add a docstring describing the package
  • Test the package import with from package import *
  • Exercise 9

    Comprehensive Module Project

    Create a complete utility package with multiple modules.

    utilities/
    ├── __init__.py
    ├── math_ops.py
    ├── string_ops.py
    ├── date_ops.py
    └── io_ops.py
  • Create a utilities package with the structure above
  • Add appropriate functions to each module
  • Customize the __init__.py to import key functions
  • Create a test script that uses all modules
  • Demonstrate the package's functionality