When the environment is modified outside Python, os.environ is not updated. I propose adding a new os.environ.refresh() method to manually update os.environ.
Note that on Windows the C runtime's Unicode and multibyte environments -- _wenviron and _environ -- can get out of sync with the process environment -- GetEnvironmentStringsW() -- due to code that directly calls WinAPI SetEnvironmentVariableW(). It's possible to implement a function to fresh the C environment on Windows, which unsets the variables in _wenviron and sets the variables from GetEnvironmentStringsW(), but that's probably not something that the Python runtime needs to worry about.
I'm not sure if this is a good idea. I suspect it will be a hard feature to get right, and as noted in the forum discussion, most beginners will misunderstand the feature.
As I noted in the discourse thread, and was mentioned in the comment on the PR, I still feel like this name will be confusing for new users, even with docs clarifying it. Can we maybe re-discuss this? Or at least choose another name?
Should we continue here or in the discourse thread?
I think @zooba's point was that this function doesn't reload the system wide environment variable settings into the running application process, which os.reload_environment() could be taken to mean.
That misreading doesn't seem particularly likely to me though, and the fact this operation reloads both os.environ and os.environb (not just os.environ) makes me wary of the shorter os.reload_environ() name (it feels misleading to me in the same way that the os.environ.refresh() spelling is misleading).
I can't reasonably see os.reload_environ() implying that os.environb wouldn't also be reloaded. Certainly not as much as I can see someone thinking that os.reload_environment() implies something other than "os.environ will be reloaded".
Whereas os.environ.refresh() could quite reasonably be assumed to have no impact on os.environb. So I think that's another argument in favour of the module-level function.
Perhaps os.reload_environs() feels more comfortable for covering both os.environ and os.environb? I don't have a strong preference about the s. My main concern is still the same as at the start - this function doesn't impact the (generic) environment of the process at all, it only affects what is contained in Python's cache of the environment, otherwise known as os.environ.
I created PR gh-126268 to replace the os.environ.refresh() method with a new os.reload_environ() function.
IMO reload_environ() is a good name since it reminds os.environ. I don't think that we should bother with the fact that os.environb is also reloaded to name the function. And reload_environment() is too long :-)
Activity
pythongh-120057: Add os.environ.refresh() method
pythongh-120057: Add os.environ.refresh() method
eryksun commented on Jun 4, 2024
Note that on Windows the C runtime's Unicode and multibyte environments -- _wenviron and _environ -- can get out of sync with the process environment -- GetEnvironmentStringsW() -- due to code that directly calls WinAPI SetEnvironmentVariableW(). It's possible to implement a function to fresh the C environment on Windows, which unsets the variables in _wenviron and sets the variables from GetEnvironmentStringsW(), but that's probably not something that the Python runtime needs to worry about.
pythongh-120057: Add os.environ.refresh() method
vstinner commented on Jun 5, 2024
As soon as there are different "caches", it's hard to keep everything in sync and consistent :-(
erlend-aasland commented on Jun 5, 2024
I'm not sure if this is a good idea. I suspect it will be a hard feature to get right, and as noted in the forum discussion, most beginners will misunderstand the feature.
vstinner commented on Jun 5, 2024
Would you mind to elaborate?
vstinner commented on Jun 5, 2024
I added "in the same process" to reduce misunderstanding.
picnixz commented on Jun 5, 2024
FYI, there was this issue: #119737 (so you may want to close it to avoid duplicates)
vstinner commented on Jun 5, 2024
I closed issue #119737 as a duplicate of this issue.
gh-120057: Add os.environ.refresh() method (#120059)
vstinner commented on Jun 11, 2024
Implemented by the commit 7aff2de.
WolfDWyc commented on Jun 11, 2024
@vstinner
As I noted in the discourse thread, and was mentioned in the comment on the PR, I still feel like this name will be confusing for new users, even with docs clarifying it. Can we maybe re-discuss this? Or at least choose another name?
Should we continue here or in the discourse thread?
21 remaining items
ncoghlan commented on Oct 31, 2024
I think @zooba's point was that this function doesn't reload the system wide environment variable settings into the running application process, which os.reload_environment() could be taken to mean.
That misreading doesn't seem particularly likely to me though, and the fact this operation reloads both os.environ and os.environb (not just os.environ) makes me wary of the shorter os.reload_environ() name (it feels misleading to me in the same way that the os.environ.refresh() spelling is misleading).
zooba commented on Oct 31, 2024
I can't reasonably see os.reload_environ() implying that os.environb wouldn't also be reloaded. Certainly not as much as I can see someone thinking that os.reload_environment() implies something other than "os.environ will be reloaded".
Whereas os.environ.refresh() could quite reasonably be assumed to have no impact on os.environb. So I think that's another argument in favour of the module-level function.
Perhaps os.reload_environs() feels more comfortable for covering both os.environ and os.environb? I don't have a strong preference about the s. My main concern is still the same as at the start - this function doesn't impact the (generic) environment of the process at all, it only affects what is contained in Python's cache of the environment, otherwise known as os.environ.
pythongh-120057: Add os.reload_environ() function
vstinner commented on Nov 1, 2024
I created PR gh-126268 to replace the os.environ.refresh() method with a new os.reload_environ() function.
IMO reload_environ() is a good name since it reminds os.environ. I don't think that we should bother with the fact that os.environb is also reloaded to name the function. And reload_environment() is too long :-)
gh-120057: Add os.reload_environ() function (#126268)
vstinner commented on Nov 5, 2024
Done: I merged the PR. I close the issue.
pythongh-120057: Add os.reload_environ() function (python#126268)
pythongh-120057: Add os.reload_environ() function (python#126268)
gh-120057: add `os.reload_environ` to `__all__` (#140763)
pythongh-120057: add `os.reload_environ` to `__all__` (pythonGH-140763)
[3.14] gh-120057: add `os.reload_environ` to `__all__` (GH-140763) (#…
pythongh-120057: add `os.reload_environ` to `__all__` (python#140763)