Andromeda
Note

Importing Modules (Python)

Definition

The mechanism for including code from external files or the Python Standard Library into your current program.

Why It Matters

Software complexity grows exponentially; importing allows it to stay manageable by ensuring programmers do not have to reinvent the wheel for every project. It creates an ecosystem where specialized knowledge (like high-level cryptography or complex data visualization) is “packaged” and made universally available, effectively lowering the barrier to entry for building sophisticated, world-class applications.

Core Concepts

import math
print(math.sqrt(16))  # Prefixed access

from random import randint
print(randint(1, 10)) # Direct access
  • import module_name: Standard way to ingest a module. Requires prefixing functions with the module name (e.g., math.sqrt()).
  • from module_name import *: Imports all functions directly into the current namespace. Danger: Can lead to “name shadowing” where your variables accidentally overwrite module functions.
  • The Standard Library: A collection of pre-written modules (e.g., random, sys, os) that come with every Python installation.
  • Third-Party Modules (PyPI): External code hosted on the Python Package Index (PyPI) and installed via the Pip Package Manager (Python). This ecosystem provides specialized tools for everything from Excel manipulation (openpyxl) to GUI automation (pyautogui).

Connected Concepts