Environment Variables in Python

Miquel Canal

Tuesday 6, October 2020
  • Python
  • Software Architecture

What are environment variables?

Environment variables are those configurations that are tied to a specific environment. During the lifecycle of software development, an application usually navigates through multiple environments (or stages) before it gets to the final deployment. Examples of environments are: local computers of developers, Development, Beta / Test and Production.

Configurations and setups are not the same across all environments. For example, the credentials to access the local database of a developer computer will be different than the one in Beta environment. Same way that the host URL of an API might change between Development and Production.

That is why keeping a clean management of environment variables is key to success. Software applications are easier to scale when they are not tied to environments and their source code is independent.

Create environment variables in Python

In Python there a build-in library named os which provides ways to access the Operating System which the library is running on. It can be used to access the environment variables that are set on a particular environment.

The os.environ variable hosts a key-value pair dictionary which can retrieve environment variables. Example:

import os

The os library allows us to read the environment variables. It also allows for setting new environment variables. As the environ is dictionary, you can set a new key-value pair to it.

import os
os.environ['new_variable']="Value of the new variable"

It is importance to notice that setting up a new environment variable as shown on the lines above will only take effect on the current running Python process. Once the Python interpreter gets restarted, the added variable on your code will not be persistent.

The easiest way to work with environment variables is to use a library such as dotenv.

Dotenv environment variables in Python

python-dotenv is a library that reads key-value pairs stored on an external .env file and adds them into the os environment variables.

The use case is simple. First, you need to create a new .env file next to the Python script. Then, load the name and value of the variables as a key-value pairs:


It is the Python script which runs the application the one that has to import the dotenv library and then call load_dotenv() function to initialise the environment variables. After that you can access environment variables from the .env file by calling os.getenv("my_variable") . Example:

# Import libraries
import os
from dotenv import load_dotenv

# Initialise the library

# Access the environment variables
database_name = os.getenv("DATABASE_NAME")
database_password = os.getenv("DATABASE_PASSWORD")


Virtual Environments in Python

Virtual Environments in Python

A look out to Python's virtual environments. Why are they useful and how can be used in software applications to install Python packages using Python Package Index (PyPI)

Best Practices in Express JS (Node.js)

Best Practices in Express JS (Node.js)

Fast, unopinionated, minimalist web framework for Node.js. The post covers best practices for developing Node.js applications on top of the Express JS web framework.

Domain Driven Design (DDD)

Domain Driven Design (DDD)

What is Domain Driven Design and why it is important to define a clear terminology? This article covers the key aspects of developing software following best practices of Domain Driven Design.

This site uses cookies to ensure a great experience. By continue navigating through the site you accept the storage of these cookies.