os module
The os module lets you talk to the operating system: read environment variables, build paths and look at folders.
Used in 16 lessons of the course, from 00.02.
os is a standard library module. You bring it in with import os, then use its functions with the os. prefix, for example os.path.join(...).
What you will see most in the course is os.environ. It is a dict-like object holding your system's environment variables. Things like API keys live there, so you read them from the environment instead of writing them in your code. That way you don't push them to a repository by accident.
To read a variable safely, use os.environ.get("NAME"). If it is missing, you get None instead of an error.
import os
print(os.environ.get("MY_VAR", "no value"))
Other handy pieces are os.getcwd() (the current folder) and os.path.join (joins path parts with the right separator for your system).
Reach for it when: you need to read an environment variable, such as an API key, or build paths that work on any operating system.
Examples
Common mistake
import os key = os.environ["CLAVE_INEXISTENTE_123"] print(key)
Why: `os.environ[...]` with square brackets raises `KeyError` when the variable does not exist, just like a dict.
How to write it: Use os.environ.get("CLAVE_INEXISTENTE_123") and check whether the result is None, or give it a default.
Common mistake
import os os.environ["PUERTO"] = 8080
Why: Values in `os.environ` must be strings. An integer is not accepted.
How to write it: Convert the value: os.environ["PUERTO"] = str(8080).
