Table of Contents
Importing Modules
A module is a Python file (.py
) containing functions, classes, and variables, etc.
Importing an Entire Module
Use the import
statement to import a module.
import math
print(math.sqrt(16)) # Using the sqrt() function from the math module
# Output:
4.0
Importing a Specific Function
You can import only the functions you need from a module. This will help reduce load time and improve code organization.
from math import sqrt, pi
print(sqrt(25)) # Using sqrt() directly
print(pi) # Accessing the pi constant
# Output:
5.0
3.141592653589793
Importing With Aliases
You can rename a module using as
for convenience.
import numpy as np
array = np.array([1, 2, 3])
print(array)
# Output:
[1 2 3]
Importing All Functions from a Module (β Not Recommended)
β Not recommended because it can overwrite existing function names amongst other things.
from math import *
print(sin(0))
# Output:
0.0
Creating & Using Packages
A package is a collection of modules organized into a directory. It must contain a special __init__.py
file.
Project File Structure & Creating packages
In your projects root folder create a directory mypackage/
and any other packages or sub-packages.
my_project/ # Parent project directory
βββ main.py # Main script (imports modules from mypackage)
βββ mypackage/ # Package directory
β βββ __init__.py # Required for package recognition
β βββ module1.py # A sample module
β βββ module2.py # Another module
β βββ subpackage/ # A subpackage inside mypackage
β β βββ __init__.py # Required for subpackage recognition
β β βββ module3.py # Module inside the subpackage
β β βββ module4.py
βββ another_package/ # Another independent package
β βββ __init__.py
β βββ helper.py
Add Functions to Modules
mypackage/module1.py
:
def greet(name):
return f"Hello, {name}!"
mypackage/'module2.py`:
def add(a, b):
return a + b
mypackage/subpackage/'module3.py`:
def subtract(a, b):
return a - b
another_package/helper.py
:
def multiply(a, b):
return a * b
Importing a Package
Use import
to access functions from the package. For this example, the package is imported into main.py
.
import mypackage.module1
import mypackage.module2
print(mypackage.module1.greet("Alice"))
print(mypackage.module2.add(3, 5))
# Output:
Hello, Alice!
8
Importing a Specific Function from
a Package Module
from mypackage.module1 import greet
from mypackage.module2 import add
print(greet("Bob"))
print(add(10, 20))
# Output:
Hello, Bob!
30
Importing Sub-package Modules
from mypackage.subpackage.module3 import subtract
print(subtract(10, 5))
# Output:
5
Importing Packages in Nested Directories
If your script is in a deeper directory and you need to import a package from a higher level, use sys.path
to adjust the import path.
Example
Assume script.py
is inside another subdirectory (my_project/scripts/script.py
), and you want to import mypackage.module1
.
my_project/
βββ main.py
βββ mypackage/
βββ another_package/
βββ scripts/
β βββ script.py
scripts/script.py
import sys
import os
# Add the parent directory (my_project) to sys.path
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), "..")))
# Now you can import from mypackage
from mypackage.module1 import greet
print(greet("Charlie"))
# Output:
Hello, Charlie!
Commonly used Modules
Mathematics & Scientific Computing
Module |
Description |
math |
Provides mathematical functions like sqrt() , sin() , cos() , and log() . |
cmath |
Similar to math , but supports complex numbers. |
random |
Generates random numbers, shuffles sequences, and selects random items. |
statistics |
Provides statistical functions like mean, median, and mode. |
decimal |
Performs high-precision decimal arithmetic, useful for financial calculations. |
fractions |
Supports rational number calculations using fractions. |
Date & Time Handling
Module |
Description |
datetime |
Provides functions for working with dates and times. |
time |
Handles time-related functions, such as sleeping, measuring execution time, and formatting time. |
calendar |
Supports calendar-related functions, such as generating and formatting calendars. |
zoneinfo |
Provides time zone support in compliance with the IANA time zone database. |
File Handling & Operating System Interaction
Module |
Description |
os |
Interfaces with the operating system for file management and environment variables. |
sys |
Provides access to system-specific parameters and functions, such as command-line arguments. |
shutil |
Supports high-level file operations like copying and moving files. |
pathlib |
Offers an object-oriented approach to working with file paths. |
tempfile |
Creates and manages temporary files and directories. |
fnmatch |
Implements filename matching using wildcard patterns. |
glob |
Finds files using shell-like wildcard matching. |
Data Serialization & Parsing
Module |
Description |
json |
Parses and generates JSON data, useful for web applications and APIs. |
csv |
Reads and writes CSV files, useful for handling spreadsheet data. |
xml.etree.ElementTree |
Parses and manipulates XML documents in a tree structure. |
configparser |
Reads and writes configuration files in .ini format. |
pickle |
Serializes and deserializes Python objects for storage or transmission. |
Networking & Internet Protocols
Module |
Description |
socket |
Provides low-level networking support for sockets and protocols. |
http.client |
Supports HTTP requests and responses at a low level. |
urllib |
Fetches data across the web using HTTP and FTP. |
requests |
A third-party module, but widely used for making HTTP requests (consider using it instead of urllib ). |
smtplib |
Sends emails using SMTP (Simple Mail Transfer Protocol). |
imaplib |
Reads and manages emails using IMAP (Internet Message Access Protocol). |
email |
Constructs, parses, and manages email messages. |
Text Processing & Regular Expressions
Module |
Description |
re |
Implements regular expressions for pattern matching in strings. |
string |
Provides string manipulation tools and predefined string constants. |
textwrap |
Formats and wraps text for improved readability. |
difflib |
Compares sequences and generates differences (useful for comparing texts). |
unicodedata |
Works with Unicode characters and provides Unicode properties. |
Concurrency & Parallelism
Module |
Description |
threading |
Implements threading for concurrent execution of tasks. |
multiprocessing |
Supports parallel processing using multiple CPU cores. |
asyncio |
Provides asynchronous programming features for handling non-blocking tasks. |
concurrent.futures |
Manages parallel execution with high-level threading and process pools. |
queue |
Implements multi-threaded queues for task management. |
Debugging, Testing & Logging
Module |
Description |
logging |
Logs messages for debugging and monitoring applications. |
traceback |
Prints and extracts stack traces for debugging. |
pdb |
Pythonβs built-in debugger for step-by-step code execution. |
unittest |
Supports unit testing and test discovery. |
doctest |
Checks code examples embedded in docstrings. |
warnings |
Manages warning messages for deprecated features and runtime issues. |
Cryptography & Security
Module |
Description |
hashlib |
Generates cryptographic hashes (MD5, SHA-256, etc.). |
hmac |
Implements message authentication codes using cryptographic hash functions. |
secrets |
Generates cryptographically secure random numbers for passwords and tokens. |
ssl |
Manages SSL and TLS encryption for secure network connections. |
Compression & File Archiving
Module |
Description |
zipfile |
Reads and writes .zip archive files. |
tarfile |
Reads and writes .tar archive files. |
gzip |
Compresses and decompresses .gz files. |
bz2 |
Provides compression using the BZ2 algorithm. |
lzma |
Implements LZMA compression for .xz files. |
Miscellaneous Utilities
Module |
Description |
itertools |
Implements iterators for efficient looping and combination generation. |
functools |
Provides higher-order functions for function manipulation. |
operator |
Supports efficient mathematical and logical operations. |
collections |
Implements advanced data structures like Counter , deque , and defaultdict . |
heapq |
Provides heap queue algorithms for priority queues. |
bisect |
Implements binary search for sorted lists. |
typing |
Introduces type hints for better code clarity and linting. |
enum |
Implements enumerations for defining constant sets of values. |