import os
In Python, environment variables are stored in the environ
of the os module.
The type is a map type.
user_name = os.environ['USERNAME']
print(user_name)
You can also get it by using os.getenv (key, default = None)
.
user_name = os.getenv('USERNAME', 'dummy')
print(user_name)
os.getenv ()
specifies the key of the environment variable in the first argument.
If the specified key does not exist, the value of the second argument is returned.
There are two ways to access environment variables.
os.environ
os.getenv ()
I think it depends, but if you just want to access it, I think it's better to use os.getenv ()
which will return an arbitrary value if the environment variable does not exist.
Recommended Posts