There was a discussion in Ideas (no need to read, I’ll summarise here) about adding a method to os.environ
to allow regenerating its contents from the current process environment. There is no clear agreement on the name.
This is important because os.environ
is technically a cache - variables are read at initialisation and are not updated again. Generally, a Python-only app is going to make changes through os.environ
, which means it will be written to the cache and everything is fine. No need for this method.
However, if you call os.putenv()
directly, or have native code that updates the environment without going through os.environ
(e.g. using ctypes
to call native putenv
, or a library that updates it), those changes will not be visible to Python code. Currently, there’s just no way to get at them other than bypassing os.environ
yourself to read the actual environment.
So there’s no dispute about the importance of this functionality - we are the only ones who can realistically provide it, and so we should. However, there’s significant dispute about the naming. You might like to see this message where @vstinner took a vote in Ideas on the options, with os.environ.refresh()
being most popular, followed by os.environ.reload()
and os.environ.reload_from_process()
.
The concerns about the name of the method are that the functionality is quite obscure, and generally is not something you would ever use until you’ve identified that your app has this issue. A name like refresh()
or reload()
potentially suggests a lot of things to a range of potential callers, but it does not suggest “reload if non-Python code in your app is changing the environment without telling you” to someone who’s browsing code completions.
(One obvious thing it may suggest is to refresh or reload the environment from the user profile. This is very much out of scope - it’s been discussed, attempted and dismissed already. There’s no way to know the parent process didn’t modify the environment before launching Python, and so resetting/changing that state may cause new problems.)
The function has already been added to 3.14 as os.environ.refresh()
(see third paragraph in the os.environ
docs), despite the naming controversy on the Ideas thread and the fact that it was never raised here.
So we’re raising it here to look for core developer consensus on renaming[1] this function. Does refresh()
adequately describe what is happening here? Will it be a tempting footgun with that name? (And any brilliant ideas on handling the fact that environ
is not really thread-safe at all on any platform…)
Or moving - one proposal was to put it directly in
os
rather than onos.environ
, though people are more likely to stumble blindly upon it inos
so I don’t think we gave it much weight. ↩︎