Site icon Full-Stack

How to Use Environment Variables in Python Projects

When building real-world Python applications, managing sensitive data like API keys, database credentials, or secret tokens securely is crucial. Hardcoding these values in your code is risky—especially when working in teams or deploying to production.

That’s where environment variables come in. They help you manage configurations outside of your source code, making your projects safer, cleaner, and easier to maintain.

What Are Environment Variables?

Environment variables are key-value pairs stored outside your code, typically in your operating system or .env file. Python can read these variables at runtime, allowing you to separate secrets and configurations from your logic.

Why Use Environment Variables?

Using Environment Variables in Python

Python’s built-in os module makes it easy to read environment variables.

Example:

python

Copy code

import os

api_key = os.getenv(“API_KEY”)

print(f”Using API Key: {api_key}”)

If the API_KEY variable is not set, os.getenv() will return None.

Setting Environment Variables

1. Temporarily (on the command line)

Linux/macOS:

bash

Copy code

export API_KEY=’your-secret-key’

python your_script.py

Windows (CMD):

cmd

Copy code

set API_KEY=your-secret-key

python your_script.py

2. Using a .env File with python-dotenv

Create a .env file:

ini

Copy code

API_KEY=your-secret-key

DEBUG=True

Install the python-dotenv package:

bash

Copy code

pip install python-dotenv

Load the .env file in your Python code:

python

Copy code

from dotenv import load_dotenv

import os

load_dotenv()

api_key = os.getenv(“API_KEY”)

Best Practices

python

Copy code

debug_mode = os.getenv(“DEBUG”, “False”) == “True”

Practice Challenge

  1. Create a .env file with:

ini

Copy code

DB_USER=admin

DB_PASS=secure123

  1. Write Python code to read these values using dotenv.

This hands-on task will help you understand how to manage secrets the right way.

Build Smarter Projects, Safer

Using environment variables is one of the first steps toward writing secure and scalable Python applications. Whether you’re working on Flask, Django, or data pipelines—this habit will pay off in every project.

👉 Learn how to apply these skills in real-world projects at https://www.thefullstack.co.in/courses/

You also like this:

What is Backend Development? A Complete Guide for Beginners [2025]

How Can SAP ERP Be Beneficial
What Are the Most Popular Backend Development Languages in 2025?

Frequently Asked Questions

What are environment variables and why are they useful in Python projects?

Environment variables are values that are set outside of a Python program, such as operating system-level variables, and can be accessed from within the program. They are useful for storing sensitive information like database credentials or API keys, and for configuring the behavior of a program. This approach helps keep sensitive information out of version control and allows for easy switching between different environments.

How do I set environment variables in my Python project?

To set environment variables, you can use the operating system’s command line interface or a .env file, depending on your development environment. For example, on Linux or macOS, you can use the export command, while on Windows, you can use the set command. You can also use libraries like python-dotenv to load variables from a .env file.

How do I access environment variables in my Python code?

To access environment variables in Python, you can use the os library, which provides a getenv function to retrieve the value of a variable. You can also use the dotenv library to load variables from a .env file. For example, you can use os.getenv(‘VARIABLE_NAME’) to get the value of a variable named VARIABLE_NAME.

What is the best way to manage environment variables across different environments?

A good approach to managing environment variables is to use separate .env files for each environment, such as development, testing, and production. You can then use a library like python-dotenv to load the correct file based on the current environment. This approach helps keep sensitive information organized and makes it easy to switch between environments.

How can I ensure that my environment variables are secure and not exposed to unauthorized users?

To keep your environment variables secure, make sure to add your .env file to your .gitignore file so that it is not committed to version control. You should also avoid hardcoding sensitive information directly in your Python code. Instead, use environment variables to store sensitive information, and use a secure method to load and store the variables, such as using a secrets manager.

Exit mobile version