Working with environment variables and .env files

4 minutes read

Environment files with proper handling provide a secure way to manage configuration settings in applications. These files allow to keep sensitive information like API keys and credentials separate from the code.

In this topic, we will learn about environment files and how to work with them through Python.

A few words on environment variables

Environment variables are dynamic values that exist as part of the operating system's environment and can be accessed by applications during runtime. These variables store configuration settings, system paths, API keys, and other information that programs need to function, allowing for different configurations without changing the code.

For example, say you have an API key you would like to read in a script. You can run

export API_KEY=value

in the shell, which will allow you to access the key in a script without pasting it directly into the code:

import os

api_key = os.getenv('API_KEY')
print(api_key) # will print 'value'

While environment variables can be set directly, environment files provide a more convenient way to manage them during development. They serve as a central location to store application-specific configurations.

.env file

Environment files (.env) store configuration settings and sensitive data as key-value pairs. They keep credentials and environment-specific variables separate from the application code. By referencing the variables, you avoid hardcoding information directly.

Here's a typical example of a .env file format with database configuration and an API key:

# Database settings
DB_HOST=localhost
DB_PORT=5432
DB_USER=admin
DB_PASSWORD=super_secret_password

# OpenAI credentials
OPENAI_API_KEY=openai_token

.env files should be excluded from version control systems (if working with git, list the .env files in .gitignore) to avoid sensitive information exposure.

Working with python-dotenv

In order to work with .env files in the scripts, first, install the python-dotenv package:

pip install python-dotenv

Then, you can access the content of the environment file (we suggest copying the example of a .env file from the previous section) as a dictionary like this:

import os
from dotenv import load_dotenv

load_dotenv() 

api_key=os.environ.get("OPENAI_API_KEY", None)
print(api_key) # will print 'openai_token'

The best practice is to load environment variables before importing any other modules that might need these variables.

By default, dotenv assumes that the .env file is in the same directory as the script, but the location can be changed as follows:

load_dotenv('/custom/path/.env')

This is particularly useful when there are multiple configuration files in the project (for example, for development, testing, and production).

Conclusion

As a result, you are now familiar with the following:

  • Environment variables are dynamic values that store configuration settings and sensitive data. They allow applications to access information like API keys and database credentials without hardcoding them in the source code.

  • .env files provide a developer-friendly way to manage environment variables by storing them as key-value pairs in a plain text file. These files should be kept out of version control (using .gitignore) to maintain security.

  • The python-dotenv package makes it easy to work with .env files in Python. By calling load_dotenv(), you can automatically load and access environment variables from a .env file.

16 learners liked this piece of theory. 1 didn't like it. What about you?
Report a typo