Skip to main content

⚙️ fastenv 🚀

Unified environment variable and settings management for FastAPI and beyond

PyPI coverage ci Ruff

Description

fastenv [fæst iː ən v] is a Python package for managing environment variables and application settings.

Environment variables are key-value pairs provided to the operating system with syntax like VARIABLE_NAME=value. Collections of environment variables are stored in files commonly named .env and called "dotenv" files. The Python standard library os module provides tools for reading environment variables, such as os.getenv("VARIABLE_NAME"), but only handles strings, and doesn't include tools for file I/O. Additional logic is therefore needed to load environment variables from files before they can be read by Python, and to convert variables from strings to other Python types.

This project aims to:

  • Replace the aging python-dotenv project with a similar, but more intuitive API, and modern syntax and tooling.
  • Implement asynchronous file I/O. Reading and writing files can be done asynchronously with packages like AnyIO.
  • Implement asynchronous object storage integration. Dotenv files are commonly kept in cloud object storage, but environment variable management packages typically don't integrate with object storage clients. Additional logic is therefore required to download .env files from object storage prior to loading environment variables. This project aims to integrate with S3-compatible object storage, with a focus on downloading and uploading file objects.
  • Read settings from TOML. It's all about pyproject.toml now. The Python community has pushed PEP 517 build tooling and PEP 518 build requirements forward, and even setuptools has come around. PEP 621 defined how to store package metadata and dependencies in pyproject.toml. Why don't we use the metadata from our pyproject.toml files in our Python applications?
  • Unify settings management for FastAPI. Uvicorn, Starlette, and pydantic each have their own ways of loading environment variables and configuring application settings. This means that, when configuring a FastAPI application, there are at least three different settings management tools available, each with their own pros and cons. It would be helpful to address the limitations of each of these options, potentially providing a similar, improved API for each one.

The source code is 100% type-annotated and unit-tested.

For additional background on the project, see www.bws.bio/projects/fastenv.

Quickstart

Install fastenv into a virtual environment:

python3 -m venv .venv
. .venv/bin/activate
python -m pip install fastenv

Then start a REPL session and try it out:

.venv  python
# instantiate a DotEnv with a variable
import fastenv
dotenv = fastenv.DotEnv("EXAMPLE_VARIABLE=example_value")
# add a variable with dictionary syntax
dotenv["ANOTHER_VARIABLE"] = "another_value"
# delete a variable
del dotenv["ANOTHER_VARIABLE"]
# add a variable by calling the instance
dotenv("I_THINK_FASTENV_IS=awesome")
# {'I_THINK_FASTENV_IS': 'awesome'}
# return a dict of the variables in the DotEnv instance
dict(dotenv)
# {'EXAMPLE_VARIABLE': 'example_value', 'I_THINK_FASTENV_IS': 'awesome'}
# save the DotEnv instance to a file
import anyio
anyio.run(fastenv.dump_dotenv, dotenv)
# Path('/path/to/this/dir/.env')

Use fastenv in your FastAPI app:

from contextlib import asynccontextmanager
from typing import AsyncGenerator, TypedDict

import fastenv
from fastapi import FastAPI, Request


class LifespanState(TypedDict):
    settings: fastenv.DotEnv


@asynccontextmanager
async def lifespan(_: FastAPI) -> AsyncGenerator[LifespanState]:
    """Configure app lifespan.

    https://fastapi.tiangolo.com/advanced/events/
    https://www.starlette.io/lifespan/
    """
    settings = await fastenv.load_dotenv(".env")
    lifespan_state: LifespanState = {"settings": settings}
    yield lifespan_state


app = FastAPI(lifespan=lifespan)


@app.get("/settings")
async def get_settings(request: Request) -> dict[str, str]:
    settings = request.state.settings
    return dict(settings)

Documentation

Documentation is built with Zensical, deployed to Vercel using the Vercel project configuration in vercel.json, and available at fastenv.bws.bio and fastenv.vercel.app.

Download files

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

Source Distribution

fastenv-0.9.0.tar.gz (18.1 kB view details)

Uploaded Source

Built Distribution

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

fastenv-0.9.0-py3-none-any.whl (17.1 kB view details)

Uploaded Python 3

File details

Details for the file fastenv-0.9.0.tar.gz.

File metadata

  • Download URL: fastenv-0.9.0.tar.gz
  • Upload date:
  • Size: 18.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.14

File hashes

Hashes for fastenv-0.9.0.tar.gz
Algorithm Hash digest
SHA256 1ce378dbdd5a06c5dcc96377e4630f42375d56918db3ed55e6b623d50f9d6af4
MD5 d29bbe051a28bc379d4fb8c611d84598
BLAKE2b-256 93c4b57494253f1bbb55b60c1f87aebf5e13b6c39674b46441e544708f3a8bee

See more details on using hashes here.

Provenance

The following attestation bundles were made for fastenv-0.9.0.tar.gz:

Publisher: ci.yml on br3ndonland/fastenv

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file fastenv-0.9.0-py3-none-any.whl.

File metadata

  • Download URL: fastenv-0.9.0-py3-none-any.whl
  • Upload date:
  • Size: 17.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.14

File hashes

Hashes for fastenv-0.9.0-py3-none-any.whl
Algorithm Hash digest
SHA256 86a033db3b6c59c4071868e4f0b326c6d9d38c2478b56b7056585b31749cae9f
MD5 9fa21c159efae5a8f5155348b4642d0f
BLAKE2b-256 c4fe4c5496de79622a7233cfb87f667f94af9cd7e22c94775057965c25f9868a

See more details on using hashes here.

Provenance

The following attestation bundles were made for fastenv-0.9.0-py3-none-any.whl:

Publisher: ci.yml on br3ndonland/fastenv

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page