Module platform cung cấp hàm truy xuất thông tin phần cứng, hệ điều hành, phiên bản interpreter.
Các hàm chính
| Hàm | Mô tả | Ví dụ kết quả |
|---|---|---|
architecture() | Kiến trúc executable (mặc định interpreter) | ('64bit', '') |
machine() | Loại máy (ví dụ: 'x86_64', 'i386') | 'x86_64' |
node() | Tên mạng máy tính | 'malhar-ubuntu' |
platform() | Chuỗi mô tả nền tảng đầy đủ | 'Linux-4.13.0-46-generic-x86_64-with-debian-stretch-sid' |
processor() | Tên processor thực | 'x86_64' |
python_build() | Tuple (build_number, build_date) | ('default', 'Oct 13 2017 12:02:49') |
python_compiler() | Compiler dùng build Python | 'GCC 7.2.0' |
python_implementation() | Triển khai Python: CPython, IronPython, Jython, PyPy | 'CPython' |
python_version() | Phiên bản Python 'major.minor.patchlevel' | '3.6.3' |
system() | Tên OS/hệ thống | 'Linux', 'Windows', 'Darwin' |
uname() | Named tuple: system, node, release, version, machine, processor | uname_result(...) |
Ví dụ sử dụng
import platform
print("System:", platform.system())
print("Release:", platform.release())
print("Version:", platform.version())
print("Machine:", platform.machine())
print("Processor:", platform.processor())
print("Python:", platform.python_version())
print("Implementation:", platform.python_implementation())
print("Compiler:", platform.python_compiler())
print("Platform:", platform.platform())
print("Uname:", platform.uname())
Ứng dụng thực tế
- Logging thông tin môi trường khi debug/error reporting.
- Điều chỉnh hành vi theo OS (path separator, system call).
- Kiểm tra phiên bản Python tối thiểu yêu cầu.
- Benchmark so sánh hiệu năng cross-platform.
Lưu ý
- Một số hàm có thể trả về chuỗi rỗng nếu không xác định được.
platform.uname()tương đối portable, trả về named tuple dễ unpack.