How to Use Python dotenv


By squashlabs, Last Updated: November 2, 2023

How to Use Python dotenv

Python dotenv is a Python library that allows you to load environment variables from a .env file into your Python project. It provides a simple and convenient way to manage sensitive information such as API keys, database credentials, and other configuration settings. This overview will guide you through the process of using python-dotenv effectively in your Python applications.

Step 1: Install python-dotenv

Before you can start using python-dotenv, you need to install the library. You can install it using pip, the Python package manager, by running the following command:

pip install python-dotenv

Related Article: How to Execute a Program or System Command in Python

Step 2: Create a .env File

To use python-dotenv, you need to create a .env file in the root directory of your project. This file will contain the environment variables that you want to load into your Python script. Each variable should be defined on a new line in the format KEY=VALUE.

Here’s an example of a .env file:


Make sure to replace your_api_key, your_database_host, your_database_user, and your_database_password with your actual values.

Step 3: Load Environment Variables

To load the environment variables from the .env file into your Python script, you need to import the dotenv module and call the load_dotenv() function. This function will automatically read the .env file and set the environment variables.

Here’s an example of how to use load_dotenv():

import os
from dotenv import load_dotenv

# Load environment variables from .env file

# Access environment variables
api_key = os.getenv('API_KEY')
db_host = os.getenv('DB_HOST')
db_user = os.getenv('DB_USER')
db_password = os.getenv('DB_PASSWORD')

# Use the environment variables in your code
# ...

In the above example, we import the os module to access the environment variables, and we import the load_dotenv() function from the dotenv module. We then call load_dotenv() to load the environment variables from the .env file.

After loading the environment variables, we can access them using the os.getenv() function, passing the variable name as an argument. In this example, we access the API_KEY, DB_HOST, DB_USER, and DB_PASSWORD variables.

Best Practices and Alternative Ideas

– It’s recommended to add the .env file to your project’s version control system, but make sure to exclude any sensitive information from the repository. You can do this by adding the .env file to your project’s .gitignore file.
– If you need to use different .env files for different environments (e.g., development, staging, production), you can create separate .env files (e.g.,, .env.staging, and load the appropriate file based on the current environment.
– You can also set default values for environment variables in case they are not defined in the .env file. You can do this by passing a default value as the second argument to the os.getenv() function. For example: api_key = os.getenv('API_KEY', 'default_api_key').

Overall, python-dotenv provides a convenient way to manage environment variables in your Python projects. By using a .env file, you can keep your sensitive information separate from your code and easily configure your application for different environments.

Related Article: How to Use Python with Multiple Languages (Locale Guide)

More Articles from the Python Tutorial: From Basics to Advanced Concepts series:

How to Suppress Python Warnings

Python warnings can clutter your code and make it harder to read. In this short guide, we'll show you two methods to suppress Python warnings and keep your code clean.... read more

How to Measure Elapsed Time in Python

Measuring elapsed time in Python is essential for many programming tasks. This guide provides simple code examples using the time module and the datetime module.... read more

How to Execute a Curl Command Using Python

Executing a curl command in Python can be a powerful tool for interacting with APIs and sending HTTP requests. This article provides a guide on how to execute a curl... read more

How to Parse a YAML File in Python

Parsing YAML files in Python can be made easy with the help of Python's yaml parser. This article provides a guide on how to parse YAML files using the PyYAML and... read more

How to Automatically Create a Requirements.txt in Python

Managing dependencies in Python is crucial for smooth software development. In this article, we will explore two methods to automatically create a requirements.txt file,... read more

How to Pretty Print a JSON File in Python (Human Readable)

Prettyprinting a JSON file in Python is a common task for software engineers. This article provides a guide on how to achieve this using the dump() and dumps()... read more