Package: fastenv
⚙️ fastenv 🚀Unified environment variable and settings management for FastAPI and beyondDescriptionEnvironment variablesare key-value pairs provided to the operating system with syntax likeVARIABLE_NAME=value. Collections of environment variables are stored in files commonly named.envand called "dotenv" files. The Python standard libraryosmodule provides tools for reading environment variables, such asos.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 agingpython-dotenvprojectwith 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 likeAnyIO.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.envfiles 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 aboutpyproject.tomlnow. The Python community has pushedPEP 517build tooling andPEP 518build requirements forward, andevensetuptoolshas come around.PEP 621defined how to store package metadata and dependencies inpyproject.toml. Why don't we use the metadata from ourpyproject.tomlfiles in our Python applications?Unify settings management for FastAPI.Uvicorn,Starlette, andpydanticeach have their own ways of loading environment variables and configuring application settings. This means that, whenconfiguring 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.QuickstartInstall fastenv into a virtual environment:python3-mvenv.venv
..venv/bin/activate
python-mpipinstallfastenvThen start a REPL session and try it out:.venv❯python# instantiate a DotEnv with a variableimportfastenvdotenv=fastenv.DotEnv("EXAMPLE_VARIABLE=example_value")# add a variable with dictionary syntaxdotenv["ANOTHER_VARIABLE"]="another_value"# delete a variabledeldotenv["ANOTHER_VARIABLE"]# add a variable by calling the instancedotenv("I_THINK_FASTENV_IS=awesome")# {'I_THINK_FASTENV_IS': 'awesome'}# return a dict of the variables in the DotEnv instancedict(dotenv)# {'EXAMPLE_VARIABLE': 'example_value', 'I_THINK_FASTENV_IS': 'awesome'}# save the DotEnv instance to a fileimportanyioanyio.run(fastenv.dump_dotenv,dotenv)# Path('/path/to/this/dir/.env')DocumentationDocumentation is built withMaterial for MkDocs, deployed onVercel, and available atfastenv.bws.bioandfastenv.vercel.app.Vercel build configuration:Build command:python3 -m pip install mkdocs-material && mkdocs build --site-dir publicOutput directory:public(default)Vercel site configurationis specified invercel.json.