How To Create And Use .env Files In Python
Last Updated :
24 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
Create A File If Not Exists In Python
In Python, creating a file if it does not exist is a common task that can be achieved with simplicity and efficiency. By employing the open() function with the 'x' mode, one can ensure that the file is created only if it does not already exist. This brief guide will explore the concise yet powerful
2 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
How to Load a File into the Python Console
Loading files into the Python console is a fundamental skill for any Python programmer, enabling the manipulation and analysis of diverse data formats. In this article, we'll explore how to load four common file typesâtext, JSON, CSV, and HTMLâinto the Python console. Whether you're dealing with raw
4 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
Run One Python Script From Another in Python
In Python, we can run one file from another using the import statement for integrating functions or modules, exec() function for dynamic code execution, subprocess module for running a script as a separate process, or os.system() function for executing a command to run another Python file within the
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