Python-Dotenv: Setting Environmental Variables

by | Python

Effectively managing environment variables is a critical aspect of programming, especially when dealing with sensitive data that should never be hardcoded into your applications. Python-dotenv, a dedicated library, comes to the rescue, streamlining this task effortlessly!

The Python-dotenv library enables developers to store configurations and secrets securely in a .env file. This allows applications to access environment variables without exposing sensitive information in your codebase. As a result, developers can separate environment-specific configurations from the application code, promoting cleaner and more maintainable code.

By leveraging this package, developers can streamline the process of securely managing configurations across different environments. They can also ensure that applications adhere to the best practices for building scalable and maintainable software.

Let’s look at how you can use this awesome library!

Python Dotenv

Getting Started with the Python Dotenv Library

Managing your environment variables properly is a key part of the 12-factor app principles for both development and production environments. By following these principles, you can build, scale, and maintain your applications reliably.

Python-dotenv is a useful library that allows you to manage environment variables in your Python applications. This is particularly helpful for managing app settings during development and maintaining secure, organized configurations.

The dotenv library reads the key-value pairs you set from a .env file and adds them to your environment variables. Loading the variables from the .env file is done early in the application’s bootstrap process, allowing the app to access and utilize the needed variables as if they were available in the actual environment.

How to Install Python Dotenv

To start using Python-dotenv, first, you’ll need to install the package using pip. Make sure you have pip installed on your system, and then run the following command:

pip install python-dotenv

For better organization, you can also install the python-dotenv module in a virtual environment. The virtual environment will help you isolate, package, and manage your projects better.

Here’s how you can create a new virtual environment and install Python-dotenv in it:

  • Navigate to your project’s root directory and run the following line of code.python3 -m venv venv source venv/bin/activate pip install python-dotenv

Once you’ve successfully installed the Python-dotenv package, you can now start integrating it into your project. Let’s look at a quick example.

  • Create a .env file in your project’s root directory or alongside your application’s settings module.
.env file or environment files
  • Add your environment variables as key-value pairs in the .env file like this:API_KEY=my_api_key SECRET_KEY=my_secret_key DEBUG=True

After creating your .env file, you’ll want to load the environment variables in your Python script or application. To achieve this, simply import the load_dotenv() function from the Python-dotenv module and call it at the beginning of your script:

from dotenv import load_dotenv

load_dotenv()

By doing this, Python-dotenv will automatically load your environment variables from the .env file, making them accessible to your application. Now you can use the os.getenv() function to access these environment variables:

import os

api_key = os.getenv('API_KEY')
secret_key = os.getenv('SECRET_KEY')

print(api_key)
print(secret_key)

Output:

my_api_key
my_secret_key

And that’s it! You’ve successfully integrated Python-dotenv into your project. This is very useful when making calls to APIs, like the GoogleNews API in the video below, in your app.

You can use the Python-dotenv library to manage your API keys properly. Now, let’s take a closer look at this library and the different tools it offers.

How to Work with .env Files

.env files store key-value pairs and allow developers to maintain a clear separation of application configuration settings from the application code itself. By utilizing .env files, you can also prevent sensitive information, like API keys, from being accidentally leaked or hard-coded into your Python scripts.

Let’s take a look at how you can work with these files:

1. How to Create A .env File

To create a .env file, simply create a new file in your project directory and name it .env. Inside this file, you can define your key-value pairs as environment variables, following the format KEY=VALUE on separate lines.

For example:

API_KEY=my_api_key
DATABASE_URL=my_database_url
SECRET_KEY=my_secret_key

Note: While managing environment variables, it’s essential to keep the .env file secure by not including it in your version control system, such as Git. To do this, add .env to your .gitignore file.

access .env variables file in gitignore to get values defined

2. How to Use Variable Expansion

Variable expansion is a powerful feature in python-dotenv that enables the use of values from other variables present within your .env file. This can help reduce repetition, improve readability, and simplify your configuration file.

To use variable expansion, simply include the name of another variable within ${VAR_NAME} in the value of the current variable.

# .env file
API_KEY=your_api_key_here
API_URL=https://yourapi.example.com?key=${API_KEY}

FIRST_NAME = John
LAST_NAME = Doe
FULL_NAME = '${FIRST_NAME} ${LAST_NAME}'

In this example, the API_URL variable will automatically be passed the value of API_KEY. This ensures that you only have to update the API key in one place, improving maintainability.

Also, the FULL_NAME variable will evaluate to John Doe.

3. How to Create Multiline Values

At times, you might encounter the need to store multiline values, such as JSON strings or long SQL queries, within your environment variables. Python-dotenv supports multiline values through the use of double or single quotes.

When defining a multiline value in your .env file, simply wrap it with double or single quotes:

# .env file
JSON_DATA="{\n\t\"key\": \"value\",\n\t\"another_key\": \"another_value\"\n}"

This allows the value to be read properly and includes newline characters and indentation. To access a multiline environment variable value, simply use os.getenv() just as you would with a single line value.

By incorporating variable expansion and multiline values, you can streamline your use of environment variables and enhance your Python project’s organization and maintainability.

How to Load Environment Variables with Python-dotenv

In this section, we will discuss the different ways you can load environment variables from the .env file. You can use either the load_dotenv() or dotenv_values() functions.

Here’s how:

1. Using load_dotenv() Function

The load_dotenv function, provided by the python-dotenv package, reads the environment variables from the .env file. Once you call the function, it automatically loads the variables in your .env file and sets them for your application.

Let’s look at an example. First, create a .env file in your project directory with the environment variables you want to load.

SECRET_KEY=mysecretkey
DATABASE_PASSWORD=mypassword

To automatically load these environment variables in your Python script, use the following code snippet:

import os
from dotenv import load_dotenv

load_dotenv()

SECRET_KEY = os.environ.get("SECRET_KEY")
DATABASE_PASSWORD = os.environ.get("DATABASE_PASSWORD")

Alternatively, you can use the os.getenv() function instead of os.environ.get() as follows:

SECRET_KEY = os.getenv("SECRET_KEY")
DATABASE_PASSWORD = os.getenv("DATABASE_PASSWORD")

With the load_dotenv function, your environment variables are now accessible in your Python script, making it easy to manage application settings and maintain security best practices.

2. Using the dotenv_values() Function

Unlike the load_dotenv() function, calling the dotenv_values() function doesn’t affect the application’s environment. Instead, it returns a dictionary filled with the key-value pairs defined in your .env file.

it follows this syntax:

import os
from dotenv import dotenv_values

config = dotenv_values(<path to .env file>)

var = config['<variable key>']

If we were to use this in our previous example, it would look like this:

from dotenv import dotenv_values()

config = dotenv_values('.env')

SECRET_KEY = config["SECRET_KEY"]
DATABASE_PASSWORD = config["DATABASE_PASSWORD"]

You can work with and iterate through the keys and values in the dictionary just like a regular Python dictionary.

How to Manage Environment Configurations with Python-Dotenv

Managing configurations is essential when developing and deploying applications. Usually, during the development process, there are several environments like development, production, testing, etc.

We can use Python-dotenv to manage and handle configuration settings for all these multiple application environments.

How to Manage Development and Production Environments with Multiple .env Files

For multiple environments, you can create separate .env files for each environment, like .env.development, .env.testing, and .env.production. When you switch the environments, you can load the respective .env file accordingly.

If you’re using a different OS for development and production environments, you can use the platform module to load the appropriate environment variables. For example:

import platfrom
from dotenv import load_dotenv

if platform.system.lower() == 'linux':
    load_dotenv('.env.production')
else:
    load_dotenv('.env.development')

The code above loads the production environment variables if the application is running on a Linux machine. If it is running on a Windows or macOS machine, it will load the development environment variables.

Alternatively, you can set an environment variable named ENVIRONMENT on your machine and use it to specify the development environment.

from dotenv import load_dotenv

env  = os.getenv('ENVIRONMENT', 'development')
load_dotenv(f'.env.{env}')

This code retrieves the environment variable ENVIRONMENT to load the right environment variables. If ENVIRONMENT is not set, it will default to the development environment.

Using Python’s dotenv library allows you to manage configurations for multiple environments effectively and securely.

By storing environment-specific configurations in separate .env files and loading them in your project, you can maintain a clear distinction between development and production settings while keeping the application’s sensitive information safe.

How to Encrypt and Deploy Your .env File with Dotenv-Vault

Encrypting your .env file is recommended, especially when you want to deploy it with your app. One option is to use python-dotenv-vault, a package that provides encryption and decryption functionality.

First, install dotenv-vault:

pip install dotenv-vault

After installing dotenv-vault, you can use it like you use the normal doten library. All you have to do is to change the import statement.

For example:

from dotenv_vault import load_dotenv, dotenv_values

Once you want to deploy the app, you can run this command below to create an encrypted file that you’ll deploy with your app.

npx dotenv-vault local build

It will create two files named .env.vault and .env.key. You can deploy the .env.vault file with your app as it contains the encrypted environment variables.

Next, copy and paste the key in the .env.key file into a new environment variable called DOTENV_KEY on your deployment server. Now, you can use the encrypted environment variables on your server.

Following these security practices and utilizing tools like python-dotenv-vault will help ensure your application’s sensitive information remains secure during deployment.

How to Use the Python-dotenv Command Line Interface

The Python dotenv library provides a versatile Command Line Interface (CLI) for managing and interacting with environment variables through .env files. This enhances your workflow with the dotenv library, enabling seamless interaction with your application’s environment variables.

You can install it using this command:

pip install "python-dotenv[cli]"

Available CLI Commands

The Python dotenv library comes with various CLI commands designed to meet different needs in managing environment variables. Some of the most commonly used commands are:

1. dotenv list: Lists all key-value pairs in the .env file.

dotenv list all key values of different environment variables

2. dotenv get KEY: Retrieves the value of a specific key in the .env file.

dotenv get key using variable expansion python dotenv

3. dotenv set KEY VALUE: Sets the value of a key in the .env file. If the key does not exist, it will create one.

dotenv set key value for such variables

4. dotenv unset KEY: Removes a key-value pair from the .env file.

These commands can be executed in common shells like bash and PowerShell, making it easy to manage and maintain environment variables in your application.

How to Manage Multiple Environments Using CLI

You can use CLI commands to perform actions specific to the respective environment. To do this, add -f <env name> after the dotenv command.

For example, if you want to list all the key-value pairs in a file named .env.dev, use the command below.

dotenv -f .env.dev list

To re-use this command, replace .env.dev with the appropriate file name for your desired environment.

The Python dotenv library’s CLI interface makes it easy to manage environment variables in various development stages. Its available commands cover essential tasks for interacting with .env files, and the CLI tool simplifies the management of multiple environments with separate files.

Final Thoughts

Python dotenv offers a seamless approach to managing environment variables. Its straightforward implementation makes it a powerful tool for maintaining the security and flexibility of projects across different environments.

The ease with which Python dotenv interacts with the .env files, in combination with Python’s inherently user-friendly nature, makes it an unrivaled tool in your developer toolkit. By fostering the separation of code and configuration, Python dotenv not only helps in improving code maintainability but also reinforces a project’s scalability.

By adopting Python dotenv, developers can enhance their workflow, minimize errors, and keep sensitive information safeguarded, making it a must-have tool in any Python project!

Frequently Asked Questions

Do I need to use quotes in the .env file?

No, you do not need to wrap the values in the .env files with quotes. You can choose to wrap them in either single or double quotes, but it’s not necessary as it has no effect on the variable.

However, if you’re writing a multi-line value or one with spaces and indentations, you have to wrap them in quotes.

Does the load_doebv() function override existing environment variables?

No, the load_dotenv function cannot override existing variables set on your PC. For example, on Windows PCs, we have variables like Path, OS, PC, etc., set on the system.

System variables

If you specify an environment variable in your .env file using any of these names, then dotenv will not work for it. Instead, it will return the default value set on the PC.

in this case, it would be better to use the to retrieve the variables.

How to set and use environment variables in a Python project?

To set and use environment variables in a Python project, you can use the os module. First, import the module and then use the os.environ dictionary to set or get environment variables. For example:

import os

os.environ['VARIABLE_NAME'] = 'some_value'
variable_name = os.getenv('VARIABLE_NAME')

To simplify this process, you can use the python-dotenv package to load environment variables from a .env file.

author avatar
Sam McKay, CFA
Sam is Enterprise DNA's CEO & Founder. He helps individuals and organizations develop data driven cultures and create enterprise value by delivering business intelligence training and education.

Related Posts