Junior — Middle
Explain what environment variables are and how they are used in applications.
sobes.tech AI
Answer from AI
Environment variables are key-value pairs stored in the operating system and accessible to processes running within that environment. They are used to pass configuration data to applications without hardcoding them into the code.
Uses of environment variables in applications:
- Storing confidential data (e.g., passwords, API tokens).
- Configuring application parameters (e.g., mode of operation — development, production).
- Specifying paths to resources or services.
Example of usage in Python:
import os
# Getting the value of an environment variable
db_password = os.getenv('DB_PASSWORD', 'default_password')
print(f"Database password: {db_password}")
Thus, environment variables allow flexible configuration of application behavior without changing the source code, which is especially important for different environments (local development, testing, production).