🎓 A1Learner
🔍

Python Built-in Modules

The standard library is the collection of ready modules that come with Python. The sys and platform modules are part of that collection.

Read the version as numbers

version_info splits the version into parts.

import sys

print(sys.version_info.major)
print(sys.version_info.minor)

Ask the system its name

The platform module describes your computer.

import platform

print(platform.system())

See what a module offers

dir() lists the names inside a module. You can test for one name with in.

import math

print("sqrt" in dir(math))
print("pi" in dir(math))

Words after the file name

sys.argv always holds at least one item. That item is the file name itself.

import sys

print(len(sys.argv) >= 1)

Example

import sys
import platform

print("My setup")
print(f"Python: {sys.version_info.major}.{sys.version_info.minor}.{sys.version_info.micro}")
print(f"System: {platform.system()}")
print(f"Version text: {platform.python_version()}")

Key points

  • Standard library: the ready modules that come with Python
  • Use import sys to work with the Python system itself
  • Use sys.version_info to read the version as numbers
  • Use platform.system() to get the name of your system
  • Use dir(module) to list the names a module offers

In this example, the program builds a small setup report. It imports sys and platform first. It reads the version parts from sys.version_info. On this machine, those parts join into the text 3.12.3. Then it asks platform for the system name, Linux here. Your own numbers and system name may differ. Last, it prints the version text from platform.

Note

sys.argv holds the words typed after the file name.

That list changes with each run, so examples here do not print it.

Try it Yourself

Edit the code, then press Run.

Running is simulated · Python Playground

Exercise

What does dir(math) give you?