Loading notes...
Loading notes...
BCA • Chapter 10
Chapter 10 explains how to structure large Python applications using Modules and Packages, how to utilize the Standard Library, and how to install external libraries using pip.
As software projects grow to encompass thousands of lines of code, maintaining everything in a single file becomes impossible. To manage complexity, Python allows you to logically organize your Python code by grouping related functions, classes, and variables into separate files called Modules. When modules are organized into directory hierarchies, they form Packages. This system is the backbone of Python's massive ecosystem of third-party libraries.
10.1 What is a Module?
A module is simply a file containing Python definitions and statements. The file name is the module name with the suffix `.py` appended. By using modules, you can write a function once in one file, and use it in dozens of other files without rewriting it.
# File: mymath.py (This is our custom module)
def add(a, b):
return a + b
def multiply(a, b):
return a * b
PI = 3.14159
# File: main.py (This is our main script)
import mymath
# Accessing functions and variables inside the module using dot notation
result = mymath.add(5, 10)
print("The sum is:", result)
print("Value of PI is:", mymath.PI)10.2 Different Ways to Import
Python offers several ways to import modules, allowing you to control how much of the module is loaded and what namespace is used.
# Standard import
import math
print(math.sqrt(16)) # Output: 4.0
# Importing with an alias
import datetime as dt
print(dt.datetime.now())
# Specific import
from random import randint, choice
print(randint(1, 100)) # Notice we don't type 'random.randint'
print(choice(['A', 'B', 'C']))10.3 The Standard Library
Python comes with 'batteries included'. When you install Python, you automatically get the Python Standard Library, a massive collection of pre-written modules for performing common tasks.
10.4 What is a Package?
A Package is a way of structuring Python's module namespace by using 'dotted module names'. Physically, a package is just a directory/folder containing multiple `.py` files. Crucially, for Python to recognize a directory as a package, it must contain a special file named `__init__.py` (it can be completely empty).
my_project/
|-- main.py
|-- my_package/ <-- This directory is a package
|-- __init__.py <-- Tells Python this is a package
|-- string_utils.py <-- A module inside the package
|-- math_utils.py <-- A module inside the packageTo import from a package, you use dot notation: `from my_package.math_utils import add`.
10.5 Third-Party Packages and pip
The true power of Python lies in its global community. There are hundreds of thousands of third-party packages available on the Python Package Index (PyPI). To install these, you use Python's package installer, called `pip`.
# Running pip in the terminal (Command Prompt/PowerShell)
# Installs the 'requests' library for making HTTP requests
pip install requests
# List all installed packages
pip list
# Uninstall a package
pip uninstall requests