How To Create And Use .env Files In Python
Last Updated :
28 Apr, 2025
In Python, a .env file is commonly used to store configuration settings, API keys, and other sensitive information. It is a plain text file with key-value pairs, and the python-dotenv library is often used to load these variables into the environment. In this article, we will explore the detailed process of creating and using .env Files in Python.
What is .Env Files In Python?
A .env file in Python is a simple text file used to store configuration settings, environment variables, and other key-value pairs related to a Python project. These files typically contain sensitive information such as API keys, database credentials, or configuration settings. The contents of a .env file are not meant to be hardcoded in the source code but are instead loaded into the project's environment during runtime. The python-dotenv library is commonly employed to read the contents of the .env file and set the environment variables, making it a convenient way to manage project configurations securely.
Installation
How To Create And Use .Env Files In Python?
Below is the complete procedure to create and use .Env files in Python:
Step 1: Create the .env file
In your project directory, create a file named ".env" at the root level to store configuration settings and sensitive information.

Step 2: Install the Module
Install the python-dotenv library by running the following command in your terminal or integrated terminal within your Python IDE (e.g., PyCharm):
pip install python-dotenv

Step 3: Store Sensitive Data in .env File
In your .env file, define key-value pairs for your configuration settings. For example:
MY_KEY = "kishankaushik12353"
This file will store sensitive information and configuration settings.

Step 4: Access the .env file
In your Python code, import the dotenv module, and use load_dotenv() to load variables from the .env file. Access the values using os.getenv("KEY") for each key-value pair defined in the .env file.
Python3
# importing os module for environment variables
import os
# importing necessary functions from dotenv library
from dotenv import load_dotenv, dotenv_values
# loading variables from .env file
load_dotenv()
# accessing and printing value
print(os.getenv("MY_KEY"))

Step 5: Compile and Run the Code
Compile and run the Python code. The program will utilize the loaded environment variables from the .env file, and the output will display the value associated with the specified key, such as "MY_KEY," demonstrating successful integration with the .env file.

Conclusion
In conclusion, utilizing .env files in Python provides a secure and organized way to manage sensitive information and configuration settings. By using the python-dotenv library, developers can easily load variables into the project's environment, enhancing flexibility across different environments. This practice ensures the separation of sensitive data from the codebase and enables a systematic approach for managing configurations in development, testing, and production. Incorporating .env files is a best practice for maintaining security and scalability in Python projects.
Similar Reads
How to Write a Configuration File in Python? Configuring your Python applications using configuration files is a common practice that allows you to separate configuration details from your code, making it more modular and easier to manage. In this article, we'll explore how to create a configuration file in Python, focusing on a simple and wid
3 min read
How Can I Make One Python File Run Another File? In Python programming, there often arises the need to execute one Python file from within another. This could be for modularity, reusability, or simply for the sake of organization. In this article, we will explore different approaches to achieve this task, each with its advantages and use cases. Ma
2 min read
Uses of OS and Sys in Python In this article, we will see where we use Os and Sys in Python with the help of code examples. What is Os Module?Python OS module in Python furnishes a versatile means of engaging with the operating system. It facilitates a range of operations, including the creation, deletion, renaming, movement, a
4 min read
Create a File Path with Variables in Python The task is to create a file path using variables in Python. Different methods we can use are string concatenation and os.path.join(), both of which allow us to build file paths dynamically and ensure compatibility across different platforms. For example, if you have a folder named Documents and a f
3 min read
How to Import Other Python Files? We have a task of how to import other Python Files. In this article, we will see how to import other Python Files. Python's modular and reusable nature is one of its strengths, allowing developers to organize their code into separate files and modules. Importing files in Python enables you to reuse
3 min read
Source distribution and built distribution in python Python is all about Distribution. It can be defined as the collection of files that together allows to build, package and distribute a module. Once a distribution exists in a system, it can easily be installed. Also, it can be shared with PyPI(Python package index) community so that other people can
5 min read
Managing Python Dependencies Managing dependencies becomes crucial to ensure smooth development and deployment processes. In this article, we will explore various methods for managing Python dependencies, from the basics of using pip to more advanced tools like virtualenv and pipenv. How to Manage Dependencies in PythonBelow ar
2 min read
Using Python Environment Variables with Python Dotenv Python dotenv is a powerful tool that makes it easy to handle environment variables in Python applications from start to finish. It lets you easily load configuration settings from a special file (usually named .env) instead of hardcoding them. This makes your code more secure, easier to manage, and
3 min read
Os Module Vs. Sys Module In Python Python provides a lot of libraries to interact with the development environment. If you need help using the OS and Sys Module in Python, you have landed in the right place. This article covers a detailed explanation of the OS and Sys Module including their comparison. By the end of this article, you
5 min read
Build a Debian package(.deb) from your Python Program Creating a Debian package (.deb file) for the Python program allows for easier distribution and installation on the Debian-based systems. Here's a step-by-step guide on how to create a .deb file for the Python program:Build a Debian package(.deb) from your Python ProgramCreating a Debian package for
2 min read