Skip to main content

pydantic-settings-file-envar

Support for loading pydantic-settings from files, using the _FILE environment variable suffix pattern commonly used in container images, especially official Docker images.

If you have a setting called foo, by default pydantic-settings will load it from an environment variable FOO. With pydantic-settings-file-envar, if an environment variable FOO_FILE=/a/b/foo is set, the secret's value will be loaded from the file at /a/b/foo.

Note that pydantic-settings has built-in support for loading secrets from files, so that may be all you need. It's different from the _FILE envar pattern in that it expects a specific directory to be specified, which contains appropriately-named secret files.

Install

$ pip install pydantic-settings-file-envar

Usage

To use _FILE envar settings, override the settings_customise_sources() class method of your settings class to include the FileSuffixEnvSettingsSource this package provides:

from pydantic_settings_file_envar import FileSuffixEnvSettingsSource

# ...

class ExampleSettings(BaseSettings):
    threshold: int
    launch_code: str

    @classmethod
    def settings_customise_sources(
        cls,
        settings_cls: Type[BaseSettings],
        init_settings: PydanticBaseSettingsSource,
        env_settings: PydanticBaseSettingsSource,
        dotenv_settings: PydanticBaseSettingsSource,
        file_secret_settings: PydanticBaseSettingsSource,
    ) -> Tuple[PydanticBaseSettingsSource, ...]:
        # Adding FileSuffixEnvSettingsSource enables _FILE envars
        return (init_settings, env_settings, FileSuffixEnvSettingsSource(settings_cls))

If you place it after the default env_settings source, _FILE environment variable will only be used if a regular environment variable is not set. The pydantic-settings docs have more details on configuring settings sources.

Example program & Docker image

The ./example dir contains an example command-line program that loads settings using regular or _FILE envars.

Try it like this:

$ cd example
$ poetry install --with local
$ poetry shell
$ echo -n secret-from-file > /tmp/secret
$ THRESHOLD=9000 LAUNCH_CODE_FILE=/tmp/secret file-envar-example
Loaded settings: threshold=9000 launch_code='secret-from-file'

Or the Docker image:

$ # build the example image
$ docker buildx bake example
[+] Building 5.2s (12/12) FINISHED                                                                         ...
 => => naming to docker.io/library/file-envar-example

$ echo -n secret-from-file > /tmp/secret
$ docker container run --rm -v /tmp:/secrets -e THRESHOLD=9000 -e LAUNCH_CODE_FILE=/secrets/secret file-envar-example
Loaded settings: threshold=9000 launch_code='secret-from-file'

Developing

See docs/dev.md.

Metadata

Release files for pydantic-settings-file-envar 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pydantic-settings-file-envar 0.1.0
File Size Uploaded Provenance Actions
pydantic_settings_file_envar-0.1.0.tar.gz 3.9 kB Without provenance Details for pydantic_settings_file_envar-0.1.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for pydantic-settings-file-envar 0.1.0
File Interpreter ABI Platform Actions
pydantic_settings_file_envar-0.1.0-py3-none-any.whl Python 3 none any Details for pydantic_settings_file_envar-0.1.0-py3-none-any.whl

Total release size: 8.4 kB

Release files / pydantic_settings_file_envar-0.1.0.tar.gz

Download URL pydantic_settings_file_envar-0.1.0.tar.gz
Size 3.9 kB
Tags Source
SHA-256 checksum
How to use checksums
4976d10037ba1bc06fd574e2e37da0d73beb145886331752558ce958df2a480a
BLAKE2b-256 checksum
How to use checksums
f4e89797ed3d8f83f6fe792dba994169134210f93fd9b110f3e128be1deaac33
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/4.0.2 CPython/3.11.5

Release files / pydantic_settings_file_envar-0.1.0-py3-none-any.whl

Download URL pydantic_settings_file_envar-0.1.0-py3-none-any.whl
Size 4.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
c50e811ff4b69254a3cdb5ce987589b2d97d576c7170c81747d10f93ee24459b
BLAKE2b-256 checksum
How to use checksums
e80927cc10eed43c15d52c09453e8d19ff97019037481f132974aaea6f44c5dc
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/4.0.2 CPython/3.11.5

Security for pydantic-settings-file-envar 0.1.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

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