Back to Curriculum

Modules and Packages

📚 Lesson 7 of 20 ⏱️ 35 min

Modules and Packages

35 min

A module is a file containing Python definitions and statements. The file name is the module name with the suffix `.py` appended. Modules allow you to organize code into logical units, making it reusable and maintainable across different projects and applications.

You can use any Python source file as a module by executing an `import` statement in some other Python source file. When you import a module, Python executes the module's code and makes its definitions available in your current namespace. This enables code reuse and separation of concerns.

Packages are a way of structuring Python's module namespace by using 'dotted module names'. For example, the module name `A.B` designates a submodule named `B` in a package named `A`. Packages are directories containing an `__init__.py` file, which can be empty or contain initialization code.

Python's import system is powerful and flexible. You can import entire modules (`import math`), specific items (`from math import sqrt`), or use aliases (`import numpy as np`). Understanding these patterns helps you write clean, readable code and avoid namespace pollution.

The `__init__.py` file in a package directory can be empty or contain initialization code. In Python 3.3+, namespace packages don't require `__init__.py`, but it's still recommended for explicit packages and to control what gets exported from the package.

Python's module search path includes the current directory, directories in PYTHONPATH, and the standard library. Understanding this search order helps debug import errors and organize your project structure. You can inspect `sys.path` to see where Python looks for modules.

Key Concepts

  • Modules are Python files containing reusable code and definitions.
  • Packages are directories containing modules and an __init__.py file.
  • Import statements make module code available in your program.
  • Python searches for modules in directories listed in sys.path.
  • Relative imports allow importing from the same package using dot notation.

Learning Objectives

Master

  • Creating and importing modules in Python
  • Understanding package structure and the role of __init__.py
  • Using different import styles (import, from...import, as aliases)
  • Organizing code into packages and subpackages effectively

Develop

  • Code organization and modularity thinking
  • Understanding Python's import system and module search path
  • Building reusable code libraries and packages

Tips

  • Use absolute imports for clarity and to avoid confusion with relative imports.
  • Keep __init__.py files simple or use them to expose a clean package API.
  • Use virtual environments to manage package dependencies and avoid conflicts.
  • Follow PEP 8 naming conventions for modules (lowercase, underscores, not hyphens).

Common Pitfalls

  • Circular imports when modules import each other, causing ImportError.
  • Naming modules the same as standard library modules (e.g., 'math.py' shadows the math module).
  • Not understanding the difference between absolute and relative imports.
  • Forgetting that importing executes module-level code, which can have side effects.

Summary

  • Modules organize code into reusable, logical units.
  • Packages structure multiple modules hierarchically using directories.
  • Import statements make module code available in your current namespace.
  • Proper module organization improves code maintainability and reusability.

Exercise

Import the `math` module and use its `sqrt` function to calculate the square root of 16.

import math\n\nresult = math.sqrt(16)\nprint(result)

Exercise Tips

  • Try importing specific functions: from math import sqrt, pi to avoid namespace pollution.
  • Explore other math module functions (ceil, floor, pow, factorial, etc.).
  • Create your own module with functions and import it to practice module creation.
  • Use 'import math as m' to see how aliases work and shorten long module names.

Code Editor

Output