Process Environment

The operating system supplies each process P with an environment, which is a set of environment variables whose names are identifiers (most often, by convention, uppercase identifiers) and whose contents are strings. For example, in Chapter 3, we covered environment variables that affect Python’s operations. Operating system shells offer various ways to examine and modify the environment, by such means as shell commands and others mentioned in Chapter 3.

The environment of any process P is determined when P starts. After startup, only P itself can change P’s environment. Nothing that P does affects the environment of P’s parent process (the process that started P), nor those of child processes previously started from P and now running, nor of processes unrelated to P. Changes to P’s environment affect only P itself: the environment is not a means of IPC. Child processes of P normally get a copy of P’s environment as their starting environment: in this sense, changes to P’s environment do affect child processes that P starts after such changes.

Module os supplies attribute environ, a mapping that represents the current process’s environment. os.environ is initialized from the process environment when Python starts. Changes to os.environ update the current process’s environment if the platform supports such updates. Keys and values in os.environ must be strings. On Windows, but not on Unix-like platforms, keys into os.environ are implicitly uppercased. For example, ...

Get Python in a Nutshell now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.