Django How To Set Environment Variables
Environs variables are used to alter the organization configuration. The output of the many Python applications depends on the values of the particular environment variables. When those environment variables change, the python script requires changing to get the appropriate output, which is not desirable. This trouble can exist solved by reading and setting the value of the environment variable in the Python script based on the requirement. It eliminates the chore of irresolute the environment variable manually and makes the lawmaking more secure by hiding the sensitive data required to assign the surround variable, such as API token. The ways to set and get the surroundings variable in Python take shown in this tutorial.
Read Environment Variables in Python:
The os module volition require to import to read the environs variables. The bone.environ object is used in Python to access the environment variable. The coder can set and get the value of any environment variable by using this object. Different ways to read, check and assign the value of the environment variable have shown in the next part of this tutorial.
Example-1: Read all and specific environs variable
Create a python file with the following script to read and impress all variables and the specific environment variable. The 'for' loop has used in the script to read and print all existing environment variable names and values. Adjacent, the value of the 'Habitation' variable has been printed.
# Import bone module
import os
# Iterate loop to read and print all environment variables
print ( "The keys and values of all environment variables:" )
for fundamental in bone.environ:
print (central, '=>' , os.environ [key] )
# Impress the value of the particular environment variable
print ( "The value of HOME is: " , os.environ [ 'Habitation' ] )
Output:
The following output will announced later on executing the above script. The list of all environs variables has been printed, and the value of the Dwelling house variable has been printed at the end of the output.
Example-2: Check the specific environment variable is set or not
Create a python file with the post-obit script to bank check the particular environment variable is set or not. Here, the os module has been used to read the values of the particular surround variable, and the sys module has been used to finish from the script. The infinite 'while' loop has continuously checked the value of the specific environment variable continuously until the user provides a variable name that is not set up. If the user provides an environs variable name as input, and then the value of that variable will be printed. If the user provides an
# Import os module
import bone
# Import sys module
import sys
while True:
# Take the proper name of the environment variable
key_value = input ( "Enter the key of the environment variable:" )
# Bank check the taken variable is fix or non
try:
if os.environ [key_value]:
print ( "The value of" , key_value, " is " , os.environ [key_value] )
# Heighten error if the variable is not set
except KeyError:
print (key_value, 'environment variable is not set up.' )
# Terminate from the script
sys.leave ( 1 )
Output:
Afterward executing the above script, the post-obit output will appear if the variable name taken is set for the first input value and not set for the second input value. According to the output, the value of the Dwelling variable is set, and the value of this variable has been printed. Next, the API_KEY has taken equally the variable that is non gear up. And so, the script has terminated after displaying the bulletin.
Instance-3: Cheque the particular environs variable is on or off
Create a python file with the following script to bank check a particular surroundings variable is on or off. The get() part has been used in the script to check the electric current value of the 'DEBUG' is True or Fake. The script will print the message based on the value of the variable.
# Import os module
import os
# Checking the value of the environment variable
if os.environ.get ( 'DEBUG' ) == 'Truthful':
impress ( 'Debug mode is on' )
else:
print ( 'Debug way is off' )
Output:
The following output will appear after executing the above script if the value of the DEBUG variable is False. The variable's value can be changed past using the setdefault() function shown in the side by side example.
Example-3: Assign the value to the environment variable
The setdefault() function is used to set the value of whatsoever surroundings variable. Create a python file with the following script to enable the environment variable, 'DEBUG', that is disabled by default. The value of this variable has been enabled at the beginning of the script by setting the value to True using the setdefault() office. Next, the value of this variable has checked by using the get() part. The message, 'Debug mode is on' will be printed if the variable is set properly; otherwise, the message, 'Debug mode is off' volition be printed.
# Import os module
import os
# Set the value DEBUG variable
os.environ.setdefault ( 'DEBUG' , 'True' )
# Checking the value of the environment variable
if bone.environ.get ( 'DEBUG' ) == 'True':
print ( 'Debug mode is on' )
else:
print ( 'Debug fashion is off' )
Output:
The post-obit output will appear afterwards executing the above script. The 'DEBUG' variable has been enabled by using setting its value to Truthful. So, the message, 'Debug mode is on' has printed as the output.
Conclusion:
The values of the environment variables can be set or get by using environ[] assortment of the os module or past using the setdefault() and the become() functions. The proper name of the environs variable is used as the index of the environ[] assortment to set or get the value of that variable. The become() function is used to become the value of a particular variable, and setdefault() office is used to gear up the value of the particular variable.
Source: https://linuxhint.com/set-environment-variables-python/
0 Response to "Django How To Set Environment Variables"
Post a Comment