Skip to main content

typed_dotenv

Parse .env files with types

Installation

pip install typed_dotenv

To use...

  • load_into: pip install typed_dotenv[pydantic]
  • YAML literals: pip install typed_dotenv[yaml]
  • TOML literals: pip install typed_dotenv[toml]

Usage

With the following .env file:

GITHUB_TOKEN="jkjkimnotputtinmygithubpersonaltokeninamodulesexamples"
DEBUG_IN_PRODUCTION=False
import typed_dotenv

secrets = typed_dotenv.load(".env")

You'll see here that nothing has changed much: secrets['DEBUG_IN_PRODUCTION'] is still a str.

That's because you need to explicitly define what syntax your .env uses. Add the following at the top of your .env:

# values: python
GITHUB_TOKEN="jkjkimnotputtinmygithubpersonaltokeninamodulesexamples"
DEBUG_IN_PRODUCTION=False

Now the following will not raise an assertion error:

import typed_dotenv

secrets = typed_dotenv.load() # ".env" is the default value
assert type(secrets["DEBUG_IN_PRODUCTION"]) is bool

We used python-style values, but other syntaxes are available:

  • values: yaml 1.2 to use YAML 1.2 literalspip install typed_dotenv[yaml]
  • values: yaml 1.1 to use YAML 1.1 literals (differences from YAML 1.2). For now, this has the same effect as values: yaml 1.2pip install typed_dotenv[yaml]
  • values: toml to use TOML literals: 12:35:24 resolves to a datetime.time, etc.pip install typed_dotenv[toml]
  • values: json to use JSON literals

Now, up until now, we've only seen how to get those variables into a dict.

The recommended way: using Pydantic's BaseModel with load_into

This way, you have IDE autcompletion and type checking, and pydantic will raise errors if the value is not of the right type:

from pydantic import BaseModel
import typed_dotenv

class Secrets(BaseModel):
    GITHUB_TOKEN: str
    DEBUG_IN_PRODUCTION: bool

secrets = typed_dotenv.load_into(Secrets, filename="my_dotenv.env")

Production usage without a dotenv file

You can also use load_into with None as the filename to load variables from the environment:

from pydantic import BaseModel
import typed_dotenv

class Secrets(BaseModel):
    GITHUB_TOKEN: str
    DEBUG_IN_PRODUCTION: bool

secrets = typed_dotenv.load_into(Secrets, filename=None)

In that case, the value syntax will always be YAML 1.2 (we need a format that allows raw strings, since env variables are unquoted).

Manually coerce from os.getenv calls

You might still want to load these values as environment variables, but need to get type coersion. This time, since the value is gotten via os.getenv, typed_dotenv does not know the file's contents. The syntax used is thus declared when coercing types:

from os import getenv
import typed_dotenv

print(os.getenv("MY_ENV_VARIABLE"))
print(
    typed_dotenv.coerce(
        os.getenv("MY_ENV_VARIABLE"),
        typed_dotenv.VALUE_FORMATS.python_literal
    )
)

You can also make yourself a function to avoid declaring the values' format each time:

def env_coerce(key: str) -> Any:
    return typed_dotenv.coerce(
        os.getenv(key),
        typed_dotenv.VALUE_FORMATS.python_literal
    )

And use it like so:

print(env_coerce("MY_ENV_VARIABLE"))

Metadata

Release files for typed_dotenv 2.0.2

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

Source distribution (sdist)

Source distribution for typed_dotenv 2.0.2
File Size Uploaded Provenance
typed_dotenv-2.0.2.tar.gz 3.7 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for typed_dotenv 2.0.2
File Interpreter ABI Platform
typed_dotenv-2.0.2-py3-none-any.whl Python 3 none any Details

Total release size: 7.8 kB

Release files / typed_dotenv-2.0.2.tar.gz

Download URL typed_dotenv-2.0.2.tar.gz
Size 3.7 kB
Tags Source
SHA-256 checksum
How to use checksums
e10350ea222326d106f7a354c9da4bc9e70cb1c371864e01c036d83bda7e4609
BLAKE2b-256 checksum
How to use checksums
4ff61b976a3d727d6559bdfc60b32fe263285365c7599067423539b31892b0cb
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.8.2 CPython/3.11.8 Linux/6.8.1-arch1-1

Release files / typed_dotenv-2.0.2-py3-none-any.whl

Download URL typed_dotenv-2.0.2-py3-none-any.whl
Size 4.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
d79d5a95a33466ca9dac0b9351bef787099f02a651ba9921000d0ff2d4626de2
BLAKE2b-256 checksum
How to use checksums
0da8fd4c7ba6aa801bea3a6c7fb236f2901dff8099754e27f80007f841374c96
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.8.2 CPython/3.11.8 Linux/6.8.1-arch1-1

Security for typed_dotenv 2.0.2

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.

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