poetry-plugin-dotenv - is the plugin that automatically loads environment variables from a dotenv file into the environment before poetry commands are run.
Project description
Overview
poetry-plugin-dotenv
- is the plugin that automatically loads environment variables from a dotenv file into the environment before poetry
commands are run.
Features
- Doesn't require any dependencies
- Supports templates, interpolating variables using POSIX variable expansions
- Fully type safe
- 100% test coverage and "A" grade for maintainability
Installation
poetry self add poetry-plugin-dotenv
Usage and Configuration
By default, the plugin will load the .env
file from the current working directory or "higher directories".
To prevent poetry
from loading the dotenv file, set the ignore
option.
If your dotenv file is located in a different path or has a different name you may set the location
.
ignore
option can accept the next values:
- As True:
y / yes / t / on / 1 / true
- As False:
n / no / f / off / 0 / false
Configuration via file
The plugin is able to read project-specific default values for its options from a pyproject.toml
file.
By default, poetry-plugin-dotenv
looks for pyproject.toml
containing a [tool.poetry.plugins.dotenv]
section.
Example pyproject.toml
:
[tool.poetry.plugins.dotenv]
ignore = "false"
location = ".env.dev"
[!IMPORTANT] Due to the default
poetry
parser, options in the plugins sections should be always strings.
Configuration via environment variables
poetry-plugin-dotenv
supports the following configuration options via environment variables.
POETRY_PLUGIN_DOTENV_LOCATION
POETRY_PLUGIN_DOTENV_IGNORE
[!IMPORTANT] Due to the nature of environment variables, options should be always strings.
Lookup hierarchy
A pyproject.toml
can override default values. Options provided by the user via environment variables override both.
Examples
# .env
DB__HOST=localhost
DB__DBNAME=local_lakehouse
DB__USER=volodymyr
DB__PASSWORD=super_secret_password
DB__ENGINE=postgresql://${DB__USER}:${DB__PASSWORD}@${DB__HOST}/${DB__DBNAME}
# .env.dev
DB__HOST=dev.host
DB__DBNAME=dev_lakehouse
DB__USER=svc_team
DB__PASSWORD=super_secret_password
DB__ENGINE=postgresql://${DB__USER}:${DB__PASSWORD}@${DB__HOST}/${DB__DBNAME}
# pyroject.toml
[tool.poetry.plugins.dotenv]
location = ".env.dev"
# main.py
from __future__ import annotations
import os
if __name__ == "__main__":
try:
print(f"Host: {os.environ['DB__HOST']!r}") # noqa: T201
print(f"Name: {os.environ['DB__DBNAME']!r}") # noqa: T201
print(f"Username: {os.environ['DB__USER']!r}") # noqa: T201
print(f"Password: {os.environ['DB__PASSWORD']!r}") # noqa: T201
print(f"Engine: {os.environ['DB__ENGINE']!r}") # noqa: T201
except KeyError:
print("Environment variables not set!") # noqa: T201
poetry run -vvv python main.py
# Loading environment variables from '.env'.
# Host: 'localhost'
# Name: 'local_lakehouse'
# Username: 'volodymyr'
# Password: 'super_secret_password'
# Engine: 'postgresql://volodymyr:super_secret_password@localhost/local_lakehouse'
# set location section in pyproject.toml
poetry run -vvv python main.py
# Loading environment variables from '.env.dev'.
# Host: 'dev.host'
# Name: 'dev_lakehouse'
# Username: 'svc_team'
# Password: 'super_secret_password'
# Engine: 'postgresql://svc_team:super_secret_password@dev.host/dev_lakehouse'
# set ignore = "true" in pyproject.toml
poetry run -vvv python main.py
# Not loading environment variables.
# Environment variables not set!
export POETRY_PLUGIN_DOTENV_LOCATION=.env.dev && poetry run -vvv python main.py
# Loading environment variables from '.env.dev'.
# Host: 'dev.host'
# Name: 'dev_lakehouse'
# Username: 'svc_team'
# Password: 'super_secret_password'
# Engine: 'postgresql://svc_team:super_secret_password@dev.host/dev_lakehouse'
export POETRY_PLUGIN_DOTENV_IGNORE=true && poetry run -vvv python main.py
# Not loading environment variables.
# Environment variables not set!
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file poetry_plugin_dotenv-2.1.3.tar.gz
.
File metadata
- Download URL: poetry_plugin_dotenv-2.1.3.tar.gz
- Upload date:
- Size: 13.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.0.0 CPython/3.11.9
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 54c2ef05551b36caf00a323ed31a9840912df5fe18594c928a1d8ea1f0d6fd14 |
|
MD5 | f55185596d0492127c08ff5255ae6f0f |
|
BLAKE2b-256 | 3976eb83a30396b55927f98584d6c68e6399613bad47f466a7aa6f9eb8c1666c |
Provenance
File details
Details for the file poetry_plugin_dotenv-2.1.3-py3-none-any.whl
.
File metadata
- Download URL: poetry_plugin_dotenv-2.1.3-py3-none-any.whl
- Upload date:
- Size: 13.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.0.0 CPython/3.11.9
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | e45c4d0111cfaa51f08ba2acd9bf2fe962627c716fd111412319d31cda181016 |
|
MD5 | 6d3eea844986774650e08251b387cc29 |
|
BLAKE2b-256 | b00d96beff104ba6741d281e2d233756eb34b59f2348494cea0b5ff0bfffeff0 |