The Python Standard Library
Python's standard library is incredibly extensive. It offers a wide range of facilities such as handling operating system functions, file systems, dates, networks, math, and much more. It's often said that Python comes with "batteries included."
Here are the most important, frequently used built-in modules every Python developer should know.
System and Files
- os — Operating system interfaces and file paths.
- sys — System-specific parameters and functions (like
sys.argv). - subprocess — Spawn new processes and execute commands.
- pathlib — Object-oriented filesystem paths.
- shutil — High-level file operations (copy, move, rmtree).
- tempfile — Generate temporary files and directories.
Data Formats & Persistence
- json — JSON encoder and decoder.
- csv — CSV File Reading and Writing.
- sqlite3 — DB-API 2.0 interface for SQLite databases.
- pickle — Python object serialization (saving objects).
Time and Math
- datetime — Basic date and time types.
- time — Time access and conversions (sleep, timestamps).
- calendar — General calendar-related functions.
- math — Mathematical functions.
- random — Generate pseudo-random numbers.
- statistics — Mathematical statistics functions.
Data Structures & Algorithms
- collections — Container datatypes (Counter, defaultdict).
- itertools — Functions creating iterators for efficient looping.
- copy — Shallow and deep copy operations.
- enum — Support for enumerations.
Architecture & Typing
- dataclasses — Auto-generate
__init__and__repr__for data objects. - typing — Support for type hints.
- functools — Higher-order functions, partials, and caching.
Error Handling & Debugging
- logging — Professional logging facility for Python.
- argparse — Parser for command-line options and flags.
- traceback — Print or retrieve stack traces.
- inspect — Inspect live objects and source code.
- unittest — Unit testing framework.
Networking & Security
- requests — External: The HTTP for Humans library.
- urllib — Native URL handling and basic HTTP.
- socket — Low-level networking interface.
- hashlib — Secure hashes and message digests.
- secrets — Cryptographically strong random generators.
- uuid — Universally Unique Identifiers (UUIDv4).
- base64 — Base16, Base32, Base64 Data Encodings.
Concurrency
- asyncio — Asynchronous I/O, event loop, and coroutines.
- threading — Thread-based parallelism.
- multiprocessing — Process-based parallelism.