Python Environment Variables
Environment Variables are variables that stored outside of the program. Mostly used for security. In our program, secret key, database url and api key are the values that we don’t want to show public. Environment Variables are variables in our environment. Different Environment have different variables. Means that other users who download and run our program in their local computer, will not see those variables stored in our environment. If they want to use those key, they will need to set their testing variables to their environment variables. Environment variables are stored with keys and values and easy to set new variables and to use those values.
Show all environment variables
in command line
Set new value in environment variables
$ export SECRET_KEY=helloenv
Python OS module
os.environ will show all variables in environment.
set new value
os.environ['SECRET_KEY'] = "hello"
get env value
dotenv python module
Now, we need to seperate env file to store env values for our program. So when we publish our program, we only need to hide env file in gitignore.
pip install python-dotenv
from dotenv import load_dotenvload_dotenv()