Learn how to organize, create, and use Python modules effectively with practical examples
A module is a file containing Python definitions and statements. The file name is the module name with the suffix .py. Modules allow you to organize code logically, reuse code across multiple programs, and avoid naming conflicts.
Tip: Think of modules as "libraries" or "toolkits" that you can import and use in your programs.
# greetings.py - a simple module
def hello(name):
"""Return a greeting message"""
return f"Hello, {name}!"
def goodbye(name):
"""Return a farewell message"""
return f"Goodbye, {name}!"
version = "1.0"
# main.py - using the greetings module
import greetings
print(greetings.hello("Alice")) # Hello, Alice!
print(greetings.goodbye("Bob")) # Goodbye, Bob!
print(f"Module version: {greetings.version}")
User-defined modules are Python files you create to organize your code. Any Python file can be imported as a module in another Python script.
mymodule.py)| Method | Syntax | Description |
|---|---|---|
| Basic import | import module |
Import entire module, access with module.function() |
| Selective import | from module import function |
Import specific functions/classes |
| Alias import | import module as alias |
Import with a different name |
| Wildcard import | from module import * |
Import all names (not recommended) |
Warning: Avoid wildcard imports (from module import *) as they can lead to naming conflicts and make code harder to understand.
For larger projects, you can organize modules into packages (directories containing an __init__.py file):
my_package/
__init__.py
module1.py
module2.py
subpackage/
__init__.py
module3.py
Python comes with a rich standard library - a collection of modules that provide common functionality. These modules are available without any additional installation.
Specialized container datatypes
Data Structures
Mathematical functions
Mathematics
Operating system interfaces
System
Generate random numbers
Utilities
Statistical calculations
Mathematics
System-specific parameters
System
The collections module provides specialized container datatypes as alternatives to Python's general-purpose built-in containers.
| Class | Description | Use Case |
|---|---|---|
Counter |
Count hashable objects | Frequency counting |
defaultdict |
Dictionary with default values | Handling missing keys |
deque |
Double-ended queue | Efficient appends/pops from both ends |
namedtuple |
Tuple with named fields | Readable, lightweight object |
OrderedDict |
Dictionary that remembers insertion order | When order matters (Python < 3.7) |
from collections import Counter, defaultdict, deque, namedtuple
# Counter - count elements
words = ['apple', 'banana', 'apple', 'orange', 'banana', 'apple']
word_count = Counter(words)
print(word_count) # Counter({'apple': 3, 'banana': 2, 'orange': 1})
# defaultdict - dictionary with default values
dd = defaultdict(int) # default value is 0
dd['a'] += 1
print(dd['a']) # 1
print(dd['b']) # 0 (default)
# deque - double-ended queue
dq = deque([1, 2, 3])
dq.appendleft(0) # add to left
dq.append(4) # add to right
print(dq) # deque([0, 1, 2, 3, 4])
# namedtuple - readable tuples
Point = namedtuple('Point', ['x', 'y'])
p = Point(10, 20)
print(p.x, p.y) # 10 20
The os and sys modules provide ways to interact with the operating system and Python runtime environment.
| Function | Description |
|---|---|
os.getcwd() |
Get current working directory |
os.listdir(path) |
List files in a directory |
os.mkdir(path) |
Create a directory |
os.remove(path) |
Remove a file |
os.rename(src, dst) |
Rename a file or directory |
os.path.join() |
Join path components intelligently |
os.path.exists() |
Check if path exists |
| Function | Description |
|---|---|
sys.argv |
Command-line arguments |
sys.exit([code]) |
Exit the program |
sys.version |
Python version information |
sys.path |
Module search path |
sys.stdin, sys.stdout, sys.stderr |
Standard streams |
import os
import sys
# OS module examples
print(f"Current directory: {os.getcwd()}")
print(f"Files in current directory: {os.listdir('.')}")
# Create a directory if it doesn't exist
if not os.path.exists('data'):
os.mkdir('data')
# Join paths safely
file_path = os.path.join('data', 'file.txt')
print(f"Full path: {file_path}")
# SYS module examples
print(f"Python version: {sys.version}")
print(f"Command line arguments: {sys.argv}")
# Exit if no arguments provided
if len(sys.argv) < 2:
print("Usage: python script.py [argument]")
sys.exit(1)
Note: For modern path handling, consider using the pathlib module which provides an object-oriented approach to filesystem paths.
The math module provides mathematical functions and constants, while the random module implements pseudo-random number generators.
| Function/Constant | Description |
|---|---|
math.sqrt(x) |
Square root of x |
math.pow(x, y) |
x raised to the power y |
math.sin(x), math.cos(x), math.tan(x) |
Trigonometric functions |
math.pi |
Mathematical constant π |
math.e |
Mathematical constant e |
math.factorial(x) |
Factorial of x |
math.gcd(a, b) |
Greatest common divisor |
| Function | Description |
|---|---|
random.random() |
Random float in [0.0, 1.0) |
random.randint(a, b) |
Random integer in [a, b] |
random.choice(seq) |
Random element from sequence |
random.shuffle(seq) |
Shuffle sequence in place |
random.uniform(a, b) |
Random float in [a, b] |
random.seed(a) |
Initialize random number generator |
import math
import random
# Math examples
print(f"Square root of 16: {math.sqrt(16)}")
print(f"5 raised to power 3: {math.pow(5, 3)}")
print(f"Value of pi: {math.pi}")
print(f"Factorial of 5: {math.factorial(5)}")
# Random examples
print(f"Random float between 0 and 1: {random.random()}")
print(f"Random integer between 1 and 10: {random.randint(1, 10)}")
fruits = ['apple', 'banana', 'cherry']
print(f"Random fruit: {random.choice(fruits)}")
# Shuffle a list
cards = ['Ace', 'King', 'Queen', 'Jack']
random.shuffle(cards)
print(f"Shuffled cards: {cards}")
# Using seed for reproducible results
random.seed(42)
print(f"Random with seed: {random.random()}") # Same every time
Security Note: The random module is not suitable for cryptographic purposes. Use the secrets module for security-sensitive applications.
Python's standard library includes many other useful modules. Here are some commonly used ones:
The datetime module supplies classes for manipulating dates and times.
from datetime import datetime, date, timedelta
# Current date and time
now = datetime.now()
print(f"Current datetime: {now}")
# Specific date
d = date(2023, 12, 25)
print(f"Christmas: {d}")
# Date arithmetic
one_week = timedelta(days=7)
next_week = now + one_week
print(f"Next week: {next_week}")
# Formatting dates
formatted = now.strftime("%Y-%m-%d %H:%M:%S")
print(f"Formatted: {formatted}")
The json module provides functions for working with JSON data.
import json
# Convert Python object to JSON string
data = {
"name": "John",
"age": 30,
"cities": ["New York", "London"]
}
json_str = json.dumps(data)
print(f"JSON string: {json_str}")
# Convert JSON string to Python object
parsed = json.loads(json_str)
print(f"Parsed data: {parsed}")
# Working with files
with open('data.json', 'w') as f:
json.dump(data, f)
with open('data.json', 'r') as f:
loaded = json.load(f)
print(f"Loaded from file: {loaded}")
The re module provides regular expression matching operations.
import re
# Search for pattern
text = "The rain in Spain"
result = re.search("rain", text)
print(f"Search result: {result.group()}") # rain
# Find all matches
all_matches = re.findall("ai", text)
print(f"All 'ai' matches: {all_matches}") # ['ai', 'ai']
# Split by pattern
split_result = re.split("\s", text) # split by whitespace
print(f"Split result: {split_result}") # ['The', 'rain', 'in', 'Spain']
# Substitute
new_text = re.sub("Spain", "France", text)
print(f"Substituted text: {new_text}") # The rain in France
Follow these best practices to write clean, maintainable, and efficient Python code using modules:
Organize imports in the following order with a blank line between each group:
# Standard library
import os
import sys
from datetime import datetime
# Third-party
import requests
import numpy as np
# Local application
from mymodule import myfunction
A well-structured module should include:
Circular imports occur when two modules import each other, which can cause problems. To avoid:
This idiom allows a module to provide code that should only run when executed directly, not when imported:
# mymodule.py
def main():
# Code to run when executed directly
print("Running as main program")
if __name__ == "__main__":
main()
Use virtual environments to manage dependencies for different projects and avoid version conflicts:
# Create a virtual environment
python -m venv myenv
# Activate on Windows
myenv\Scripts\activate.bat
# Activate on macOS/Linux
source myenv/bin/activate
# Install packages in the virtual environment
pip install requests numpy
# Deactivate when done
deactivate
Add docstrings to your modules, functions, and classes to make them more understandable:
def calculate_area(radius: float) -> float:
"""
Calculate the area of a circle.
Args:
radius (float): The radius of the circle
Returns:
float: The area of the circle
"""
import math
return math.pi * radius ** 2
Here are some valuable resources to deepen your understanding of Python modules:
Tip: The best way to learn about modules is to practice creating your own and exploring the Python standard library. Try to read the source code of well-maintained open-source projects to see how they organize their code.