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
Built Distribution
File details
Details for the file python-readenv-0.7.tar.gz
.
File metadata
- Download URL: python-readenv-0.7.tar.gz
- Upload date:
- Size: 8.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.10.12
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | b1d4e2522b90d85e8bd92c8a25b701eb2651ef7548168e7caf2b4e8346455dc5 |
|
MD5 | f93c38ef0a93da9d4ae486cb5d0847d3 |
|
BLAKE2b-256 | 45648437e5f731a55262dd859f0a0f4f38fcf58383f7b08c5d9d8d9fab916af7 |
File details
Details for the file python_readenv-0.7-py3-none-any.whl
.
File metadata
- Download URL: python_readenv-0.7-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
Algorithm | Hash digest | |
---|---|---|
SHA256 | ecdc118b3e76265630f746070a6cd3b89e5230ecff851b0cab38eb3f31331676 |
|
MD5 | c3be198757248a9fb08d1da3d0a00ba4 |
|
BLAKE2b-256 | 6bcfbfe8ae6fcc855c605b5f90744f8f6e1319864e91b7f7d00d2d20708dc4be |