Generic selectors
Exact matches only
Search in title
Search in content
Post Type Selectors
Filter by Categories
About Article
Analyze Data
Archive
Best Practices
Better Outputs
Blog
Code Optimization
Code Quality
Command Line
Daily tips
Dashboard
Data Analysis & Manipulation
Data Engineer
Data Visualization
DataFrame
Delta Lake
DevOps
DuckDB
Environment Management
Feature Engineer
Git
Jupyter Notebook
LLM
LLM
Machine Learning
Machine Learning
Machine Learning & AI
Manage Data
MLOps
Natural Language Processing
NumPy
Pandas
Polars
PySpark
Python Tips
Python Utilities
Python Utilities
Scrape Data
SQL
Testing
Time Series
Tools
Visualization
Visualization & Reporting
Workflow & Automation
Workflow Automation

Secure Configuration Management with Python-dotenv

Table of Contents

Secure Configuration Management with Python-dotenv

The Problem with Hard-Coding Configuration

Hard-coding configuration values in code can lead to security risks and deployment challenges. For example, consider the following code snippet:

PASSWORD=123
USERNAME=myusername

This approach has several drawbacks:

  • Sensitive data is exposed in the code
  • Changes to configuration values require code changes

Introducing Python-dotenv

Python-dotenv is a library that allows you to load environment variables from a .env file. This approach separates configuration from code and provides several benefits:

  • Keep sensitive data out of code
  • Use different configurations per environment

To use Python-dotenv, you’ll need to create a .env file with your configuration values. Here’s an example:

#.env
PASSWORD=123
USERNAME=myusername

Next, you can load the environment variables from the .env file using the load_dotenv() function:

from dotenv import load_dotenv
import os 

load_dotenv()

PASSWORD = os.getenv('PASSWORD') # 123
USERNAME = os.getenv('USERNAME') # myusername

This code loads environment variables from a .env file, allowing secure access to configuration values like PASSWORD and USERNAME.

Link to python-dotenv

Leave a Comment

Your email address will not be published. Required fields are marked *

0
    0
    Your Cart
    Your cart is empty
    Scroll to Top

    Work with Khuyen Tran

    Work with Khuyen Tran