In this blog post, we will show you how to create and use a Python environment file (.env) and store variables.
An environment file is a simple text file that contains a list of environment variables, which are key-value pairs that are used to configure various aspects of a software application or operating system.
In Python, environment files are typically used to store sensitive information like API keys and passwords that should not be hardcoded into the application’s source code.
When an application is run, the environment variables in the environment file are loaded into the application’s runtime environment, allowing the application to access the values of the variables. This allows for easy configuration of the application without having to modify and recompile the source code.
A common format for Python environment files is a simple list of key-value pairs, with each pair on a separate line, separated by an equals sign. For example:
In Python, you can use the python-dotenv library to easily load these variables into your script and use them as such
from dotenv import load_dotenv
api_key = os.getenv("API_KEY")
secret_key = os.getenv("SECRET_KEY")
debug = os.getenv("DEBUG")
In most cases, these files are called .env and located at the root of the project for example, if your script is located at /my_project/script.py, then the env file should be located at
It’s important to note that you should never check in the .env file in to source control as it contains sensitive data.
Leave a Reply