KKAIROS
Practical guideIllustrative3 min read

Python os.environ: read environment variables

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.

Published:

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.

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):

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:

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):

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:

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):

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

Command:

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.

Ad

Source

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.

All full guides: All guides