The Question
Is it possible to split os.environ
into defaults environment variables and custom addon variables?
for example, using syntax of sets representing the key
/ ENV_VAR_NAME
:
custom_addon_env_var_keys = set(os.environ.keys()) - set(os.environ.default.keys()) # is there this `os.environ.default` kinda thing?
Is there some kind of os.environ.default
to get pure environment variables? after the program has started? (that is, exclude all the custom keys I’ve added since the program started)
What I’ve tried
I’ve tried look into the source code of os
module, the environ
is initialized by a function _createenviron
, but it’s been deleted as soon as the environ
been initialized, so it’s impossible to re-initialize a environ_2
and do the job, and copying the whole initializing function looks like a dumb way.
What I’m expecting
The following code gives a brief-view of the expecting behavior
I’m using this for cross file variables, so saving a original copy at start is not what I need
import os
# I'm using this for cross file variables
# so it is impossible to save a original copy at start
ORIGINAL_ENV_COPY = os.environ.copy()
os.environ["MY_CUSTOM_ENV_KEY"] = "HelloWorld"
# the "ORIGINAL_ENV_COPY" should be a method of getting the original version of "os.environ"
custom_addon_env_var_keys = set(os.environ.keys()) - set(ORIGINAL_ENV_COPY.keys())
print(custom_addon_env_var_keys) # output: {"MY_CUSTOM_ENV_KEY"}
Head Cracked is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.