# Python os.environ: read environment variables

Published: 2026-11-30

Rights: original
Source: https://kairos-2z6s.onrender.com/res/python-os-environ-full-en
`os.environ` behaves like a dictionary of environment variables. `os.environ.get('NAME', default)` returns the default when the variable is not set, while `os.environ['NAME']` raises `KeyError`.

Each command below is the exact command that was run and each output block is copied from the recorded log. Test ids, log hashes and exit statuses are in the evidence table.

(Test: 3 tests were run, TE-87-A, TE-87-B and TE-87-C. The expected non-zero exit is TE-87-C (exit status 1). Every row has a recorded log, and the exit statuses and log hashes are in the evidence table.)

### TE-87-A: environ.get returns the default for an unset name, environ[] raises KeyError

Setup for this run, inside a throwaway directory (`mktemp -d`):

```bash
cat > s.py <<'EOS'
import os
print(os.environ.get('DEMO_FLAG'))
print(os.environ.get('MISSING_FLAG', 'fallback'))
try:
    os.environ['MISSING_FLAG']
except KeyError:
    print('KeyError')
EOS
```

Command:

```bash
DEMO_FLAG=on python3 s.py
```

Recorded output:

```
on
fallback
KeyError
```

Exit status: 0. The output matches the expected text written before the run.

### TE-87-B: a variable set in os.environ is seen by a child process

Setup for this run, inside a throwaway directory (`mktemp -d`):

```bash
cat > s.py <<'EOS'
import os, subprocess
os.environ['KX_DEMO'] = 'child'
print(subprocess.run(['bash', '-c', 'echo $KX_DEMO'], capture_output=True, text=True).stdout.strip())
EOS
```

Command:

```bash
python3 s.py
```

Recorded output:

```
child
```

Exit status: 0. This status and the output match what was written before the run.

### TE-87-C: expected non-zero exit: os.environ[name] for a variable that is not set raises KeyError when nothing catches it

Setup for this run, inside a throwaway directory (`mktemp -d`):

```bash
cat > s.py <<'EOS'
import os
os.environ['MISSING_FLAG']
EOS
```

Command:

```bash
python3 s.py
```

Recorded output: none, standard output was empty.

Standard error from the same run:

```
Traceback (most recent call last):
  File "/tmp/b22-l7v5ymcv/work/s.py", line 2, in <module>
    os.environ['MISSING_FLAG']
  File "/usr/lib/python3.10/os.py", line 680, in __getitem__
    raise KeyError(key) from None
KeyError: 'MISSING_FLAG'
```

Exit status: 1. This status and the output match what was written before the run. The path in the traceback line is a temporary folder and differs on each machine and each run.

TE-87-B: the variable KX_DEMO was set inside the program, and the child bash process printed its value, child. TE-87-C: os.environ['MISSING_FLAG'] was not caught. The program ended with exit status 1 and the last line of standard error is KeyError: 'MISSING_FLAG'.

## Limits

TE-87-A reads. TE-87-B sets one variable and reads it in one child process. Deleting a variable was not run.

## Source

- https://docs.python.org/3/library/os.html#os.environ

## Evidence table

| Test | What it checks | Exit | Verdict | Log sha256 (first 12) |
|---|---|---|---|---|
| `TE-87-A` | environ.get returns the default for an unset name, environ[] raises KeyError | 0 | PASS | `e926466e7b59` |
| `TE-87-B` | a variable set in os.environ is seen by a child process | 0 | PASS | `aa4c9580b198` |
| `TE-87-C` | expected non-zero exit: os.environ[name] for a variable that is not set raises KeyError when nothing catches it | 1 | PASS | `2c51d328213c` |

Recorded on: bash 5.1.16(1)-release; git version 2.34.1; Python 3.10.12. Locale C.UTF-8, TZ=UTC.
Every row has a recorded log holding the setup commands, the command, stdout, stderr and exit status. The first 12 characters of the log's sha256 are in the table.

## Related guides
- [Python lru_cache: remember function results](https://kairos-2z6s.onrender.com/res/python-lru-cache-full-en)
- [Python sys.exit: set the exit status](https://kairos-2z6s.onrender.com/res/python-sys-exit-full-en)
- [Python sorting: sorted, sort and the key argument](https://kairos-2z6s.onrender.com/res/python-sorting-full-en)
- [Python Raise: signal an error on purpose](https://kairos-2z6s.onrender.com/res/python-raise-full-en)
- [Python pathlib: join paths, read a file and list a folder](https://kairos-2z6s.onrender.com/res/python-pathlib-full-en)
- [Python modules and imports](https://kairos-2z6s.onrender.com/res/python-modules-imports-full-en)
