Skip to main content

Python library for managing configuration data from environment variables using PEP 526 annotations.

Getting Started

Ecological lets you read and convert environment variables according to your configuration class definition.

For example, imagine that your application has a configurable integer port, a boolean debug flag, and a string log_level that defaults to INFO. You could declare your configuration as follows:

class Configuration(ecological.Config):
    port: int
    debug: bool
    log_level: str = "INFO"

And then set the environment variables PORT, DEBUG and LOG_LEVEL. Ecological will automatically set the class properties from the environment variables with the same (but upper cased) name.

By default, Ecological sets the values at class definition time and assigns them to the class itself, so you do not need to instantiate the class. If needed, you can change this behavior (see the Autoloading section).

Tutorial

You can use the tutorial to explore the library’s basic features interactively.

Typing Support

You can use Ecological with several types defined in PEP484, for example:

class Configuration(ecological.Config):
    list_of_values: List[str]

Will automatically parse the environment variable value as a list.

Prefixed Configuration

You can also decide to prefix your application configuration, for example, to avoid collisions:

class Configuration(ecological.Config, prefix='myapp'):
    home: str

In this case, the home property will be read from the MYAPP_HOME environment variable.

Nested Configuration

Ecological.Config also supports nested configurations, for example:

class Configuration(ecological.Config):
    integer: int

    class Nested(ecological.Config, prefix='nested'):
        boolean: bool

This way you can group related configuration properties hierarchically.

Advanced

Fine-grained Control

You can control some behavior of how the configuration properties are set.

You can achieve this by providing an ecological.Variable instance as the default value for an attribute, or by specifying global options at the class level:

my_source = {"KEY1": "VALUE1"}

class Configuration(ecological.Config, transform=lambda v, wt: v, wanted_type=int, ...):
    my_var1: WantedType = ecological.Variable(transform=lambda v, wt: wt(v), source=my_source, ...)
    my_var2: str
    # ...

All available options and their meanings are described in the table below:

Option

Class level

Variable level

Default

Description

prefix

yes

no

None

A prefix that is uppercased and prepended when a variable name is derived from an attribute name.

variable_name

yes

yes

Derived from attribute name and prefixed with prefix if specified; uppercased.

When specified on the variable level it states the exact name of the source variable that will be used.

When specified on the class level it is treated as a function that returns a variable name from the attribute name with the following signature:

def func(attribute_name: str, prefix: Optional[str] = None)

default

no

yes

(no default)

Default value for the property if it isn’t set.

transform

yes

yes

A source value is casted to the wanted_type In case of non-scalar types (+ scalar bool) the value is Python-parsed first.

A function that converts a value from the source to the value and wanted_type you expect with the following signature:

def func(source_value: str, wanted_type: Union[Type, str])

source

yes

yes

os.environ

Dictionary that the value will be loaded from.

wanted_type

yes

yes

str

Desired Python type of the attribute’s value.

On the variable level it is specified via a type annotation on the attribute: my_var_1: my_wanted_type.

However it can be also specified on the class level, then it acts as a default when the annotation is not provided:

class MyConfig(ecological.Config, wanted_type=int, ...)

The following rules apply when options are resolved:

  • when options are specified on both levels (variable and class), the variable ones take precedence over class ones,

  • when some options are missing on the variable level, their default values are taken from the class level,

  • it is not necessary to assign an ecological.Variable instance to change the behavior; it can still be changed on the class level (globally).

Autoloading

You can defer or disable autoloading of variable values by specifying the autoload option in the class definition.

On class creation (default)

When you do not provide an option, values are loaded immediately on class creation and assigned to class attributes:

class Configuration(ecological.Config):
    port: int
# Values already read and set at this point.
# assert Configuration.port == <value-of-PORT-env-var>

Never

When you choose this option, no autoloading happens. To set variable values, you must call the Config.load method explicitly:

class Configuration(ecological.Config, autoload=ecological.Autoload.NEVER):
    port: int
# Values not set at this point.
# Accessing Configuration.port would throw AttributeError.

Configuration.load()
# Values read and set at this point.
# assert Configuration.port == <value-of-PORT-env-var>

On object instance initialization

If you prefer to load and store attribute values on the object instance instead of the class itself, the Autoload.OBJECT strategy can be used:

class Configuration(ecological.Config, autoload=ecological.Autoload.OBJECT):
    port: int
# Values not set at this point.

config = Configuration()
# Values read and set at this point on ``config``.
# assert config.port == <value-of-PORT-env-var>
# Accessing ``Configuration.port`` would throw AttributeError.

Caveats and Known Limitations

  • Ecological doesn’t support (public) methods in Config classes

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

ecological-3.1.0.tar.gz (29.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

ecological-3.1.0-py3-none-any.whl (9.6 kB view details)

Uploaded Python 3

File details

Details for the file ecological-3.1.0.tar.gz.

File metadata

  • Download URL: ecological-3.1.0.tar.gz
  • Upload date:
  • Size: 29.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.6 {"installer":{"name":"uv","version":"0.11.6","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"26.04","id":"resolute","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for ecological-3.1.0.tar.gz
Algorithm Hash digest
SHA256 e48ea134c78d5cc551580be9041a7a5b4e15d0d9ddde8b8d2973d9deb683e021
MD5 a2474c8a25df20d02b5bc9497f1adf51
BLAKE2b-256 1991a1d7901978cf76ecb1c0ecacfb5cb11329974f0c4e4cbe3b18dc5ccb4b11

See more details on using hashes here.

File details

Details for the file ecological-3.1.0-py3-none-any.whl.

File metadata

  • Download URL: ecological-3.1.0-py3-none-any.whl
  • Upload date:
  • Size: 9.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.6 {"installer":{"name":"uv","version":"0.11.6","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"26.04","id":"resolute","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for ecological-3.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 9fb7de93a04ffaca45de1b0d45d82b1242ed6abde1c43a8dc54b0995a8a7757a
MD5 2d22e1cacb4ed231bfb3d935641b353c
BLAKE2b-256 9e70b6e887b4f3356e1539ea486fce4ef39312e2709fd02d4244ff5b7764ff2f

See more details on using hashes here.

Release history Release notifications | RSS feed

This release

3.1.0 This release

2 files

3.0.1

1 file

3.0.0

1 file

2.0.0

2 files

1.6.0

2 files

1.5.0

1 file

1.4.0

1 file

1.3.0

1 file

1.2.1

1 file

1.2

1 file

1.1

1 file

1.0

1 file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page