dotenv Python

Introduction

Let me quote the site https://12factor.net/ru/config

Store configuration in the environment

App configuration is everything that may vary between deploys (development environment, staging and production deploy). This includes:

  1. Connection credentials for resources such as databases, caches and other third-party services
  2. Credentials for connecting to external services, for example Amazon S3 or Twitter
  3. Values that depend on the deploy environment, such as the canonical hostname

Sometimes applications store configuration as constants in the code. This is a violation of the twelve-factor methodology, which requires a strict separation of configuration and code. Configuration can vary substantially between deploys, code should not vary.

A litmus test of whether configuration and application code are properly separated is the fact that the application's codebase can be made publicly available at any moment without compromising any private data.

Note that this definition of “configuration” does not include internal application configuration, such as ‘config/routes.rb’ in Rails, or how the main modules will be wired in Spring. This type of configuration does not change between deploys, so it is best kept in the code.

Another approach to configuration is to use configuration files that are not checked into version control, such as ‘config/database.yml’ in Rails. This is a huge improvement over using constants that are stored in the code, but this method still has drawbacks: it is easy to accidentally commit a configuration file to the repository; there is a tendency for configuration files to be scattered in different places and in different formats, which makes it difficult to review and manage all settings in one place. In addition, the formats of these files are, as a rule, specific to a particular language or framework.

A twelve-factor app stores configuration in environment variables (often shortened to env vars or env). Environment variables are easy to change between deploys without changing code; unlike configuration files, they are less likely to be accidentally committed to the code repository; and unlike custom configuration files or other configuration mechanisms such as Java System Properties, they are a standard independent of language and operating system.

Another approach to managing configuration is grouping. Sometimes applications group configuration into named groups (often called “environments”) named after a particular deploy, such as the development, test and production environments in Rails. This method does not scale well enough: the more different deploys of the application are created, the more new environment names are needed, for example staging and qa. As the project grows further, developers may add their own special environments, such as joes-staging, resulting in a combinatorial explosion of configurations that makes managing the application's deploys very fragile.

In a twelve-factor app, environment variables are independent controls, where each environment variable is completely independent of the others. They are never grouped together into “environments”, but instead are managed independently for each deploy. This is a model that scales gradually along with the natural emergence of more deploys of the application over its lifetime.

Installation

python -m pip install python-dotenv

Collecting python-dotenv Downloading python_dotenv-1.0.1-py3-none-any.whl.metadata (23 kB) Downloading python_dotenv-1.0.1-py3-none-any.whl (19 kB) Installing collected packages: python-dotenv Successfully installed python-dotenv-1.0.1

A simple example

Let's create a file .env next to the script dotenv_ex.py

# .env POSTGRES_USER=tester HOST=127.0.0.1 PORT=5432 ENDPOINT=${HOST}:${PORT}

# dotenv_ex.py import os from dotenv import load_dotenv load_dotenv() print(os.getenv('POSTGRES_USER')) print(os.getenv('ENDPOINT'))

python dotenv_ex.py

tester
127.0.0.1:5432

dotenv_values

The same result can be obtained using dotenv_values

# dotenv_ex.py from dotenv import dotenv_values config = dotenv_values(".env") print(config["POSTGRES_USER"]) print(config["ENDPOINT"])

The type of the value returned by dotenv_values() is <class 'collections.OrderedDict'>

.env.secret

Instead of one file .env let's create two .env.secret and .env.shared

# .env.secret TELEGRAM=aofeed

# .env.shared HOST=testsetup.ru PORT=80 ENDPOINT=${HOST}:${PORT}

# dotenv_ex.py from dotenv import dotenv_values config = { **dotenv_values(".env.secret"), **dotenv_values(".env.shared") } print(config)

python dotenv_ex.py

{'TELEGRAM': 'aofeed', 'HOST': 'testsetup.ru', 'PORT': '80', 'ENDPOINT': 'testsetup.ru:80'}

Import

Let's look at the get_url() function

# dotenv_ex.py from dotenv import dotenv_values def get_url() -> str: config = { **dotenv_values(".env.secret"), **dotenv_values(".env.shared") } return config["ENDPOINT"] if __name__ == "__main__": print(get_url())

python dotenv.py

testsetup.ru:80

If you now import the dotenv_ex.py module from another directory and call the get_url() function, nothing will work.

dotenv_demo/ |-- configs | |-- .env.secret | |-- .env.shared | `-- dotenv_ex.py |-- src | `-- main.py

from dotenv_demo.configs import dotenv_ex print(dotenv_ex.get_url())

python main.py

Traceback (most recent call last): File "C:\Users\A\dotenv_demo\src\main.py", line 4, in <module> print(dotenv_ex.get_url()) ^^^^^^^^^^^^^^^^^^^ File "C:\Users\A\dotenv_demo\configs\dotenv_ex.py", line 11, in get_url return config["ENDPOINT"] ~~~~~~^^^^^^^^^^^^ KeyError: 'ENDPOINT'

The interpreter will look for the .env.secret and .env.shared files in the directory src and will not be able to find them.

To explicitly specify the path to the required files, let's use os.path.join()

# dotenv_ex.py import os from dotenv import dotenv_values def get_url() -> str: path_to_secret = os.path.join(os.path.dirname(__file__), ".env.secret") path_to_shared = os.path.join(os.path.dirname(__file__), ".env.shared") config = { **dotenv_values(path_to_secret), **dotenv_values(path_to_shared) } return config["ENDPOINT"] if __name__ == "__main__": print(get_url())

python main.py

testsetup.ru:80

Article author: Andrei Olegovich

Banner image