Python Dotenv (Explanation with Examples)

In this article, I am going to discuss everything that you need to know about Python Dotenv. This module is very important when you’re working with your websites or any kind of application.

If you don’t want to expose such as keys, passwords, and confidential information you should need to hide from the outside world and keep all your secrets during and after the development of your projects.

Also read: Power Function In Python Using A Loop

Why dotenv is required?

In Python, the dotenv package is required because it is a great way to store and hide passwords, confidential data and information, and data that is very important in your code.

The dotenv allows you to create new environment variables in a (.env) file instead of putting it in your actual code.

What is Python-Dotenv?

In Python, Dotenv is a module which allows you to specify variables in an environment. In a traditional UNIX-like (dot-env) or “ .env” file inside of your project directory.

Furthermore, dotenv is helpful to developers to hide their SECRETS and KEYS without exposing confidential information to the outside world, and keeping them safe during and after the development.

Lastly, dotenv is a (npm package) which is used to automatically load (environment variables) from a (.env) file into the process.

Python Dotenv Environment (.env) variables

The environment variables are a set of key-value pairs for the current user environment. This is generally set by the (OS or Operating System) and the configurations of a current specific user.

Installation

#Create a new virtual environment
python3 -m venv venv

#activate
source venv/bin/activate

#install
pip install python-dotenv

Setting-up Python Dotenv Module

1. Create a new environment (.env) file, then load the name and value of each variable as (key-value) pairs.

#.env file

ID = "987654321"

SECRET_KEY = "hsagakrlakajeahah"

2. Open your favorite Python code editor and create main.py file, then import and call python-dotenv.

# main.py

## importing the load_dotenv from the python-dotenv module

from dotenv import load_dotenv

load_dotenv()

3. Call and access the environment (.env) variables

from dotenv import load_dotenv

import os 

#import os from dotenv import

#provides ways to access the Operating System and allows us to read the environment variables

load_dotenv()

my_id = os.getenv("ID")

my_secret_key = os.getenv("SECRET_KEY")

def myEnvironment():
    print(f'My id is: {my_id}.')
    print(f'My secret key is: {my_secret_key}.')

if __name__ == "__main__":
    myEnvironment()

Output:

ID = "987654321"

SECRET_KEY = "hsagakrlakajeahah"

KEY-NOTE: Security vulnerabilities are the main problem of many companies and developers in terms of the development of applications all over the world. But it can be resolved right away if you take good care of leaked credentials.

Then the python-dotenv helps a lot in developing a project to become safer to work with, during and after the development of the project.

IMPORTANT TIPS: If ever you’ve been accidentally exposed your (SECRET / KEYS). Just stay calm because you can generate anytime a new key.

Lastly, I highly recommend generating new keys and setting the environment variables as a safety measure before deployment of your project and adding env to your gitignore file.

What does Load_dotenv () do?

A load_dotenv() function is to look for a file named (.env) in a current directory and all the variable definitions will add to the (Operating System or OS).

How do I know if Python-dotenv is installed?

It is very simple to know if you have already installed a Python-dotenv path package in your local computer by only typing and running the pip show python-dotenv command in your terminal window.

As you can see in the above image, I haven’t installed a dotenv package on my local computer. It is a sample of how to check if you have a Python-installed on your local computer.

Where is .env file located?

The (.env) file is located in the main directory of the project. A project directory can explicitly defined as the (–file) option or a (COMPOSE_FILE) variable in an environment.

Or else, it is on the current directory where docker command is composed and executed (+1.28) or with 12-factor principles.

Conclusion

I hope this lesson has helped you learn a lot. Check out my previous article about Linspace Python for more life-changing tutorials that could help you a lot.

Leave a Comment