How Do you Get and Set Environment Variables in Python
January 11 2023
You are able to set and get environment variables in Python you can just use the os module.
import os
os.environ["VARIABLE_NAME"] = "variable value"
os.environ["VARIABLE_NAME"] = "variable value"
To get the value of an environment variable, use the os.environ.get() method:
import os
variable_value = os.environ.get("VARIABLE_NAME")
variable_value = os.environ.get("VARIABLE_NAME")
This will return the value of the environment variable if it exists, or None if it does not.
You can also use os.environ["VARIABLE_NAME"] directly but it will raise keyerror if the variable does not exist.
Comments (0)