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.6.1.tar.gz (8.6 kB view details)

Uploaded Source

Built Distribution

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

python_readenv-0.6.1-py3-none-any.whl (10.4 kB view details)

Uploaded Python 3

File details

Details for the file python-readenv-0.6.1.tar.gz.

File metadata

  • Download URL: python-readenv-0.6.1.tar.gz
  • Upload date:
  • Size: 8.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.10.12

File hashes

Hashes for python-readenv-0.6.1.tar.gz
Algorithm Hash digest
SHA256 6f01ee7f28f570eb67bdeed1c813d5210bb215f0536366b489393e3b5aafa15e
MD5 ce997b7f85f2350c55e8255148304d71
BLAKE2b-256 37439f4a95493c845199b47efd8c0bc2234f49070968586ee0f45242daca7fcb

See more details on using hashes here.

File details

Details for the file python_readenv-0.6.1-py3-none-any.whl.

File metadata

  • Download URL: python_readenv-0.6.1-py3-none-any.whl
  • Upload date:
  • Size: 10.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.10.12

File hashes

Hashes for python_readenv-0.6.1-py3-none-any.whl
Algorithm Hash digest
SHA256 f4b61f3c6f702443616ecfa2e5527c07e6e2b63ce0d3199bf7bb263f6fb587bb
MD5 16ce29503e3c24394674eaa739beeeae
BLAKE2b-256 f108d3e151292633841c0d4094035e6d71f5f4490e5fe92d6283d99f85f6bc4c

See more details on using hashes here.

Supported by

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