Skip to main content

read an env file and export to os.environ

Project description

python-readenv

readenv makes it easy to automatically load environment variables from .env file(s) and put into os.environ.

Install

$ pip install python-readenv

Getting started

You can automatically load at startup time with the helper import import readenv.loads, which try to locate and load the first env file found from your current working directory up to root. By default it will search for .env and .env.local files.

Automatic load

You can automatically load at startup time with the helper import

import readenv.loads

...

which try to locate and load the first env file found from your current working directory up to root.

Manual load

Alternatively, you can customize which files readenv should search and load

import readenv

readenv.load("myenv", "myenv.local")

mypy integration

If you need to load the environment from mypy you could add

[mypy]
plugins = readenv.mypy

in your mypy.ini or setup.cfg file.

pyproject.toml configuration is also supported:

[tool.mypy]
plugins = ["readenv.mypy"]

Custom environment

You can create your own environment

import readenv

env = readenv.Environ()

or start with the current environ copy

import copy
import os
import readenv

env = Environ(copy.deepcopy(os.environ))

Examples

Django integration

Put the helper import as first place

manage.py

#!/usr/bin/env python3

import readenv.loads  # noqa: F401 isort:skip

import sys


if __name__ == "__main__":
    readenv.setdefault("DJANGO_SETTINGS_MODULE", "myproject.settings")
    from django.core.management import execute_from_command_line

    execute_from_command_line(sys.argv)

wsgi.py

import readenv.loads  # noqa: F401 isort:skip
from django.core.wsgi import get_wsgi_application


readenv.setdefault("DJANGO_SETTINGS_MODULE", "myproject.settings")
application = get_wsgi_application()

asgi.py

import readenv.loads  # noqa: F401 isort:skip
from django.core.asgi import get_asgi_application


readenv.setdefault("DJANGO_SETTINGS_MODULE", "myproject.settings")
application = get_asgi_application()

Project details


Download files

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

Source Distribution

python-readenv-0.7.tar.gz (8.8 kB view hashes)

Uploaded Source

Built Distribution

python_readenv-0.7-py3-none-any.whl (10.4 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page