The Risks of Hardcoding
Embedding sensitive data directly in your source code exposes it to unnecessary risk and limits flexibility. Consider the following Python code snippet:- If the code is pushed to a public repository, your credentials are exposed to…
If the code is pushed to a public repository, your credentials are exposed to everyone.
- The static configuration ties the code to a single environment, forcing manua…
The static configuration ties the code to a single environment, forcing manual updates for production deployments.
Leveraging Environment Variables
Environment variables allow you to externalize sensitive configuration details. By setting these values at the operating system level, your application can automatically retrieve the correct configuration for the current environment.Accessing Environment Variables in Python
Create a simple file (e.g.,example.py) to demonstrate accessing an environment variable:
Configuring Environment Variables
On Windows
- Open Advanced System Settings and click Environment Variables.
-
Create a new user variable (e.g.,
MY_DB_URL) with a value likelocalhost:5432. -
Open a new command prompt, then verify by running:
On macOS/Linux
Set an environment variable in the terminal:Managing Multiple Variables with .env Files
For projects with numerous environment variables, managing them manually can be tedious. A common solution during development is to use an environment file (commonly named.env).
Using Pydantic BaseSettings for Validation
Pydantic offers a robust solution for managing and validating environment variables through theBaseSettings class. This method ensures that all required settings are present and automatically handles type conversions.
Create a configuration file (e.g., config.py):
Creating the .env File
In your project root, add a.env file:
Avoid committing your
.env file to version control. Add .env to your .gitignore to protect your sensitive data.Integrating Environment Variables into Your Application
After centralizing your configuration using environment variables, update your codebase to reference these settings.Database Connection Setup
Indatabase.py, adjust your database connection configuration to use environment variables:
OAuth2 Token Configuration
Similarly, update your OAuth2 settings to reference configuration variables:config.py, your application automatically adapts to different environments without modifying the code.
Summary
- Avoid Hardcoding: Embed sensitive information as environment variables rather than hardcoding.
- Environment Variables: Utilize OS-level variables to manage configurations dynamically.
- Pydantic Validation: Employ Pydantic’s
BaseSettingsto validate and manage environment settings. - .env File Usage: During development, use a
.envfile to simplify configuration management, but exclude it from version control. - Dynamic Application Configuration: Update your application to utilize environment variables, ensuring secure and flexible deployments across various environments.