Unlock Secrets to Managing Your Credentials with “Python-dotenv”: Quickstart Guide

Sunny Bhaveen Chandra
4 min readMar 29, 2023

--

As a Python developer or data scientist, you may find yourself working with sensitive information such as API keys, passwords, and other credentials that need to be kept confidential. It’s crucial to manage these credentials securely and efficiently to prevent unauthorized access or leaks. One powerful tool for managing your credentials in Python is the python-dotenv library. In this article, we will explore the basics of python-dotenv and how it can help you manage your credentials with ease.

For a slightly advanced video tutorial of this blog, you can refer to the following video —

Unlock Secrets to Managing Your Credentials with “Python-dotenv”: Quickstart Guide

What is python-dotenv?

python-dotenv is a Python library that allows you to load environment variables from a .env file in your project directory. An environment variable is a variable that is set in your computer's environment and can be accessed by programs running on your computer. Environment variables are often used to store sensitive information that should not be hard-coded into your code.

Using python-dotenv, you can store your sensitive information in a .env file and load it into your Python script at runtime. This allows you to keep your sensitive information separate from your code and reduces the risk of accidentally exposing your credentials.

Important Note: You must add .env into your .gitignore file to prevent .env from getting published to your GitHub repository.

How to Install python-dotenv

Before we dive into the details of how to use python-dotenv, let's first install it. You can install python-dotenv using pip, the package installer for Python. Open a terminal and enter the following command:

pip install python-dotenv

Creating a .env File

Once you have installed python-dotenv, you can create a .env file in your project directory to store your environment variables. A .env file is a plain text file that contains a list of key-value pairs in the format KEY=VALUE. Each key-value pair represents an environment variable that you want to set.

For example, let’s say you have an API key that you want to use in your Python script. You can store the API key in your .env file like this:

API_KEY=1234567890abcdefg

Make sure not to include trailing whitespaces after keys. And its safe to keep the value in double quotes if it contains special characters like =

Loading Environment Variables

To load the environment variables from your .env file, you can use the load_dotenv() function from python-dotenv. The load_dotenv() function looks for a .env file in the current working directory and loads the environment variables from it.

Here’s an example of how to load the environment variables from your .env file:

from dotenv import load_dotenv
load_dotenv()

After running this code, you can access the environment variables in your Python script using the os.getenv() function.

import os
api_key = os.getenv("API_KEY")

In this example, the os.getenv() function retrieves the value of the API_KEY environment variable from the current environment.

Using Environment Variables

Once you have loaded your environment variables into your Python script, you can use them just like any other variables. For example, if you want to use the API key from the example above, you can include it in your API request like this:

import requests
import os
api_key = os.getenv("API_KEY")
url = "https://api.example.com/v1/data"
headers = {"Authorization": f"Bearer {api_key}"}
response = requests.get(url, headers=headers)

In this example, we’re using the requests library to make an API request. We're setting the Authorization header to include our API key, which we retrieved from the environment variables using os.getenv().

To learn how to connect to the OpenAI API for text completion feature to generate text based on a prompt by managing your OpenAI API credentials with python-dotenv You can refer to the video I mentioned at the beginning. Code for the same is given in reference [2]

Conclusion

Managing your credentials securely is essential, especially when working with sensitive information. With python-dotenv, you can store your credentials in a .env file and load them into your Python script at runtime. This allows you to keep your sensitive information separate from your code, reducing the risk of accidental exposure.

In this article, we covered the basics of python-dotenv, including how to install it, create a .env file, and load environment variables into your Python script. We also demonstrated how to use environment variables in your code to make API requests.

python-dotenv is a powerful tool for managing your credentials in Python. By using it, you can keep your credentials secure and avoid accidentally exposing sensitive information in your code. If you're a Python developer or data scientist, python-dotenv is definitely worth considering as part of your workflow.

In summary, managing your credentials securely is crucial in today’s digital age. By using python-dotenv, you can take a step towards keeping your sensitive information safe and secure, and gain peace of mind knowing that your credentials are being managed in a safe and efficient way.

References:

Additional resources:

[1] For setting up a complete environment for python projects:

[2] Playlist to create-test-publish python packages and more —

[3] My YouTube Channel for more —

I hope these resources are helpful!

More blogs are on the way, till then keep on learning! and keep on exploring!!

--

--