← All notes

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

Sources

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

Permanent ID: iil:note:z68zjr38

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.},
}

Markdown

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.

All artifact files (JSON index)

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.