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.5.tar.gz (7.7 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.5-py3-none-any.whl (10.2 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: python-readenv-0.5.tar.gz
  • Upload date:
  • Size: 7.7 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.5.tar.gz
Algorithm Hash digest
SHA256 4db0b1ecc59d34e5108790d26d9cfac960c54ada5b95abb0386ba18e68af21bf
MD5 85bde69b4ec28d55d159436e3824b1a4
BLAKE2b-256 af80626b6bb42993253807da1e8b65c1306fa3a621a1284ed05e04b79bd2d79d

See more details on using hashes here.

File details

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

File metadata

  • Download URL: python_readenv-0.5-py3-none-any.whl
  • Upload date:
  • Size: 10.2 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.5-py3-none-any.whl
Algorithm Hash digest
SHA256 99c4831387514f6841fc20d19e77320ee3d7e31a30c58750edc78b1c57ab5ece
MD5 a03109ea6d0732378de55ee814e98463
BLAKE2b-256 c4dff4db64313c3c9526a600d4eb4ae6648d0f98e3cdc91ff8e4d9bd5f808306

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