os.environ is a string mapping of the process environment, captured at import
Answer: os.environ is a mapping whose keys and values are strings that represent the process environment. The mapping is captured the first time the os module is imported. Changes made to the environment after that point are not reflected in os.environ, except for changes made by modifying os.environ itself.
Writing values and getenv
You can query or modify the environment through this mapping. putenv() will be called automatically when the mapping is modified. On Windows, keys are converted to uppercase. Deleting an item unsets the variable and calls unsetenv().
os.getenv(key, default=None) returns that variable's string value when the key exists, otherwise default. Because getenv uses os.environ, it is also captured on import and may not reflect later environment changes made outside this mapping.
See also
Claims
- id: C1 claim: os.environ is captured the first time the os module is imported. source: S1 evidence: "This mapping is captured the first time the"
- id: C2 claim: On Windows, environ keys are converted to uppercase. source: S1 evidence: "keys are converted to uppercase"
- id: C3 claim: os.environ is a mapping of string keys and values for the process environment. source: S2 evidence: "mapping` object where keys and values are strings"
- id: C4 claim: getenv returns the environment variable key as a string, or a default. source: S2 evidence: "Return the value of the environment variable key as a string"
Sources
- [S1] os — Miscellaneous operating system interfaces (environ). Python docs. https://docs.python.org/3/library/os.html . License: PSF-2.0. Accessed 2026-11-17.
- [S2] os.rst environ and getenv (CPython docs). python/cpython (GitHub). https://github.com/python/cpython/blob/main/Doc/library/os.rst . License: PSF-2.0. Accessed 2026-11-17.
License: this note is CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Summary by iinventedlight (AI-assisted). Sources: Python docs, os — Miscellaneous operating system interfaces (environ), PSF-2.0 (https://docs.python.org/3/license.html); python/cpython (GitHub), os.rst environ and getenv (CPython docs), PSF-2.0 (https://raw.githubusercontent.com/python/cpython/main/LICENSE). Text was condensed and changed from the originals.
License: CC-BY-4.0 · Summary by iinventedlight (AI-assisted). Sources: Python docs, os — Miscellaneous operating system interfaces (environ), PSF-2.0 (https://docs.python.org/3/license.html); python/cpython (GitHub), os.rst environ and getenv (CPython docs), PSF-2.0 (https://raw.githubusercontent.com/python/cpython/main/LICENSE).
Cite this
iinventedlight. 2026. "os.environ is a string mapping of the process environment, captured at import." iinventedlight Library, iil:note:z68zjr38, version of 2026-11-17. https://iinventedlight.com/library/notes/python-os-environ/. License: CC-BY-4.0.
@misc{iil_note_z68zjr38,
author = {{iinventedlight}},
title = {{os.environ is a string mapping of the process environment, captured at import}},
howpublished = {iinventedlight Library},
year = {2026},
date = {2026-11-17},
url = {https://iinventedlight.com/library/notes/python-os-environ/},
note = {Permanent ID iil:note:z68zjr38. License: CC-BY-4.0.},
}
Spotted a mistake? Use the MCP feedback tool or email hello@iinventedlight.com. Or Report a correction (no account needed).
Extra files beside this note
These files use this note's license, shown below. Paper extras retain their reproduction status; a link is not a reproduction.
- python-os-environ.table.json (table; CC-BY-4.0)
- python-os-environ.schema.json (schema; CC-BY-4.0)
For language models and agents: start at /llms.txt | /agents.md | /.well-known/agent-library.json. This page in Markdown: /library/notes/python-os-environ.md.