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.1.tar.gz (9.4 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.7.1-py3-none-any.whl (10.6 kB view details)

Uploaded Python 3

File details

Details for the file python_readenv-0.7.1.tar.gz.

File metadata

  • Download URL: python_readenv-0.7.1.tar.gz
  • Upload date:
  • Size: 9.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.0

File hashes

Hashes for python_readenv-0.7.1.tar.gz
Algorithm Hash digest
SHA256 9f862db7598a9cb723944d1140d2dc192a732013f66cb4a3127a006aa7dd3e18
MD5 c5ec65f67c2ce17197702151863c2e02
BLAKE2b-256 a4ab92114a91a84674c21e8f8ff915a8a2bacbe44ddb7b2eff215aafe68cd888

See more details on using hashes here.

File details

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

File metadata

  • Download URL: python_readenv-0.7.1-py3-none-any.whl
  • Upload date:
  • Size: 10.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.0

File hashes

Hashes for python_readenv-0.7.1-py3-none-any.whl
Algorithm Hash digest
SHA256 3d9dd9e25e6718a2fdd1effe3b2b1597881a44bfa1e1872f13046addbdd950e3
MD5 4560d54da0e17cd0de54a032684b0cd0
BLAKE2b-256 eb490d1187d3d7df6bd58b6dd3103475c06c63b042110eacf0fe07f0f5dfe020

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