Commonly used built-in modules | Python "built-in battery" quick start
Python takes "batteries included (built-in batteries, ready to use out of the box)" as its core design concept. The standard library covers more than 90% of daily development scenarios, without additionalpip installcan be called directly. This article has sorted out 10 types of high-frequency practical built-in modules, and attached minimalist code examples to help you quickly get started and solve practical problems.
1. System interaction module
sys: Working with the Python interpreter
sysAllows us to directly interact with the interpreter and system environment, such as viewing versions, processing command line parameters, or adjusting module search paths.
os + pathlib: Manipulate files and directories gracefully
osA cross-platform system call interface is provided, but it is prone to errors when splicing paths. It is more recommended to use it firstpathlib(Python 3.4+), which uses object-oriented path design to make the code clearer and safer.
pathlibMinimalist usage
2. Date and time processing
datetime: Core date and time operations
datetimeThe module provides objects such as "date + time", "pure date", "pure time" and "time difference", and supports formatted output and string parsing.
time: Timestamps and performance timing
timeThe module is at a lower level, and common scenarios are to obtain Unix timestamp and accurately measure the time consuming of code snippets.
3. Data serialization module
json: Common data exchange format
The preferred format when interacting with frontends and other languages.jsonOnly Python Basic types can be serialized (dict、list、str、int、float、bool、None)。
pickle: Python proprietary serialization
You can serialize almost any Python object (class, function, instance, etc.), but only for Python use and do not load pickle files from unknown sources (a security risk).
4. Mathematics and random number module
math:Basic mathematical operations
Provides commonly used mathematical functions and constants such as pi, trigonometric functions, logarithms, and combinatorial numbers.
random: Pseudo-random number generation
Commonly used for random sampling, shuffling order, generating test data, etc. If you need cryptographically secure random numbers in a production environment, please usesecretsmodule.
5. Data structure enhancement module
collections: Extended container type
Provides a Python base container (list、dict、tuple、set) is an enhanced version that specifically solves performance or functional shortcomings in specific scenarios.
6. Debugging and performance analysis module
logging:Structured logging
CompareprintA more professional debugging tool that supports level, output target, and unified format recording methods.
Summarize
The "battery built in" of the Python standard library is sufficient for most daily tasks. The benefits of using them first are obvious:
- No additional installation required, project dependencies are cleaner;
- Proven stability, guaranteed performance and reliability;
- Naturally cross-platform, no need to care about system differences;
- The official documentation is complete, making it easier to get started and troubleshoot.
Only when the standard library really cannot meet the needs (such as complex HTTP requests usingrequests, for large-scale data processingpandas), and then introducing third-party libraries will make your project more robust and efficient.

