Python Glossary Русский

all Python 3osterm

os.environ

environment

The dictionary of environment variables. It supports get(), item assignment and pop(). Changes are inherited by child processes.

Syntax

os.environ  # os._Environ (MutableMapping)
os.environ.get(key, default=None)
os.environ[key] = value

It is a snapshot taken when os is first imported, not a live view: changes made behind its back — through os.putenv or C code — are not visible (since 3.14 os.reload_environ re-reads them). Keys and values must be strings: os.environ['N'] = 1 raises TypeError. On Windows keys are upper-cased. Removing a key with del or pop unsets the variable for child processes too.

Examples

import os
print(os.environ.get('HOME'))  # → ? домашний каталог, например /home/user
print(os.environ.get('MISSING', 'default'))  # → default
os.environ['MY_VAR'] = 'hello'  # видна этому процессу и его потомкам
print('MY_VAR' in os.environ)  # → True

See also

Open in the full glossarydocs.python.orgReport an inaccuracy