Python Modules and Packages
Modules and packages are essential for organizing and reusing code in Python. They allow developers to break down large programs into smaller, manageable components, making code more modular, readable, and maintainable.
1. What is a Module?​
A module is a file containing Python code (.py) that can define
functions, classes, and variables. Modules enable code reuse and
organization by grouping related functionality.
Creating a Module​
Create a file named my_module.py with the following content:
# file: my_module.py
def add(a, b):
return a + b
Using the Module​
You can import and use the module in another script:
import my_module
print(my_module.add(2, 3)) # Output: 5
- Explanation:
- The
importstatement loads the module. - Functions and variables defined in the module are accessed using the module name as a prefix.
- The
2. import, from, as Keywords​
Python provides flexible ways to import modules and their attributes.
import: Import the Entire Module​
import math
print(math.sqrt(16)) # Output: 4.0
- Explanation:
- The
importstatement loads the entiremathmodule. - Functions like
sqrtare accessed using the module name.
- The
from ... import: Import Specific Attributes​
from math import pi
print(pi) # Output: 3.141592...
- Explanation:
- The
from ... importsyntax imports specific attributes directly, avoiding the need for the module prefix.
- The
as: Alias a Module or Function​
import math as m
print(m.factorial(5)) # Output: 120
- Explanation:
- The
askeyword assigns an alias to the module, making it easier to reference.
- The
3. Built-in Modules​
Python includes a rich set of built-in modules for various tasks.
Examples:​
math: Mathematical functions.datetime: Date and time manipulation.os: Operating system interactions.random: Random number generation.
import random
print(random.randint(1, 10)) # Output: Random integer between 1 and 10
- Explanation:
- The
randommodule provides functions for generating random numbers.
- The
4. Creating Packages​
A package is a directory containing multiple modules and a special
__init__.py file. Packages allow you to organize modules into
namespaces.
Package Structure​
my_package/ ├── init.py ├── math_utils.py └── string_utils.py
Accessing a Module from a Package​
from my_package import math_utils
print(math_utils.add(2, 3)) # Output: 5
- Explanation:
- The
__init__.pyfile makes the directory a package. - Modules within the package can be imported using the package name.
- The
5. Using __name__ == "__main__"​
The __name__ variable allows a module to be both imported and run as a
standalone script.
# file: my_module.py
def greet():
print("Hello!")
if __name__ == "__main__":
greet()
- Explanation:
- When the module is run directly,
__name__is set to"__main__", and thegreet()function executes. - When imported, the
ifblock is skipped.
- When the module is run directly,
6. The dir() Function​
The dir() function lists all attributes and methods of an object or
module.
import math
print(dir(math))
- Explanation:
dir(math)returns a list of all attributes and functions in themathmodule.
7. Installing External Packages​
External libraries can be installed using pip, Python's package
manager.
Install a Package​
pip install requests
Usage​
import requests
response = requests.get("https://api.github.com")
print(response.status_code) # Output: 200
- Explanation:
- The
requestslibrary simplifies HTTP requests. - After installation, it can be imported and used like any other module.
- The
Conclusion​
This document covers the fundamentals of Python modules and packages, including creating and importing modules, using built-in modules, organizing code into packages, and installing external libraries. By mastering these concepts, you can write modular, reusable, and maintainable Python code. Modules and packages are indispensable tools for scaling your projects and leveraging Python's vast ecosystem of libraries.