# python-readenv
`readenv` makes it easy to automatically load environment variables from `.env` file(s) and put into `os.environ`.
## Install
```shell
$ 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
```python
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
```python
import readenv
readenv.load("myenv", "myenv.local")
```
#### mypy integration
If you need to load the environment from mypy you could add
```ini
[mypy]
plugins = readenv.mypy
```
in your `mypy.ini` or `setup.cfg` [file](https://mypy.readthedocs.io/en/latest/config_file.html).
[pyproject.toml](https://mypy.readthedocs.io/en/stable/config_file.html#using-a-pyproject-toml-file) configuration is also supported:
```toml
[tool.mypy]
plugins = ["readenv.mypy"]
```
## Custom environment
You can create your own environment
```python
import readenv
env = readenv.Environ()
```
or start with the current environ copy
```python
import copy
import os
import readenv
env = Environ(copy.deepcopy(os.environ))
```
## Examples
### Django integration
Put the helper import as first place
#### `manage.py`
```python
#!/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`
```python
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`
```python
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()
```
Raw data
{
"_id": null,
"home_page": "",
"name": "python-readenv",
"maintainer": "",
"docs_url": null,
"requires_python": ">=3.8",
"maintainer_email": "",
"keywords": "env,envvar,environ,environment,environment variable",
"author": "",
"author_email": "Raffaele Salmaso <raffaele.salmaso@gmail.com>",
"download_url": "https://files.pythonhosted.org/packages/45/64/8437e5f731a55262dd859f0a0f4f38fcf58383f7b08c5d9d8d9fab916af7/python-readenv-0.7.tar.gz",
"platform": null,
"description": "# python-readenv\n\n`readenv` makes it easy to automatically load environment variables from `.env` file(s) and put into `os.environ`.\n\n## Install\n\n```shell\n$ pip install python-readenv\n```\n\n## Getting started\n\nYou can automatically load at startup time with the helper import `import readenv.loads`,\nwhich try to locate and load the first env file found from your current working directory up to\nroot.\nBy default it will search for `.env` and `.env.local` files.\n\n#### Automatic load\n\nYou can automatically load at startup time with the helper import \n\n```python\nimport readenv.loads\n\n...\n```\n\nwhich try to locate and load the first env file found from your current working directory up to\nroot.\n\n#### Manual load\n\nAlternatively, you can customize which files `readenv` should search and load\n\n```python\nimport readenv\n\nreadenv.load(\"myenv\", \"myenv.local\")\n```\n\n#### mypy integration\n\nIf you need to load the environment from mypy you could add\n\n```ini\n[mypy]\nplugins = readenv.mypy\n```\n\nin your `mypy.ini` or `setup.cfg` [file](https://mypy.readthedocs.io/en/latest/config_file.html).\n\n[pyproject.toml](https://mypy.readthedocs.io/en/stable/config_file.html#using-a-pyproject-toml-file) configuration is also supported:\n\n```toml\n[tool.mypy]\nplugins = [\"readenv.mypy\"]\n```\n\n## Custom environment\n\nYou can create your own environment\n\n```python\nimport readenv\n\nenv = readenv.Environ()\n```\n\nor start with the current environ copy\n\n```python\nimport copy\nimport os\nimport readenv\n\nenv = Environ(copy.deepcopy(os.environ))\n```\n\n## Examples\n\n### Django integration\n\nPut the helper import as first place\n\n#### `manage.py`\n\n```python\n#!/usr/bin/env python3\n\nimport readenv.loads # noqa: F401 isort:skip\n\nimport sys\n\n\nif __name__ == \"__main__\":\n readenv.setdefault(\"DJANGO_SETTINGS_MODULE\", \"myproject.settings\")\n from django.core.management import execute_from_command_line\n\n execute_from_command_line(sys.argv)\n```\n\n#### `wsgi.py`\n\n```python\nimport readenv.loads # noqa: F401 isort:skip\nfrom django.core.wsgi import get_wsgi_application\n\n\nreadenv.setdefault(\"DJANGO_SETTINGS_MODULE\", \"myproject.settings\")\napplication = get_wsgi_application()\n```\n\n#### `asgi.py`\n\n```python\nimport readenv.loads # noqa: F401 isort:skip\nfrom django.core.asgi import get_asgi_application\n\n\nreadenv.setdefault(\"DJANGO_SETTINGS_MODULE\", \"myproject.settings\")\napplication = get_asgi_application()\n```\n",
"bugtrack_url": null,
"license": "MIT",
"summary": "read an env file and export to os.environ",
"version": "0.7",
"project_urls": {
"Bug Reports": "https://github.com/rsalmaso/python-readenv/discussions",
"Changelog": "https://github.com/rsalmaso/python-readenv/blob/main/CHANGELOG.md",
"Documentation": "https://github.com/rsalmaso/python-readenv/blob/main/README.md",
"GitHub": "https://github.com/rsalmaso/python-readenv",
"Home Page": "https://pypi.org/project/python-readenv/"
},
"split_keywords": [
"env",
"envvar",
"environ",
"environment",
"environment variable"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "6bcfbfe8ae6fcc855c605b5f90744f8f6e1319864e91b7f7d00d2d20708dc4be",
"md5": "c3be198757248a9fb08d1da3d0a00ba4",
"sha256": "ecdc118b3e76265630f746070a6cd3b89e5230ecff851b0cab38eb3f31331676"
},
"downloads": -1,
"filename": "python_readenv-0.7-py3-none-any.whl",
"has_sig": false,
"md5_digest": "c3be198757248a9fb08d1da3d0a00ba4",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.8",
"size": 10450,
"upload_time": "2024-02-22T13:24:53",
"upload_time_iso_8601": "2024-02-22T13:24:53.658761Z",
"url": "https://files.pythonhosted.org/packages/6b/cf/bfe8ae6fcc855c605b5f90744f8f6e1319864e91b7f7d00d2d20708dc4be/python_readenv-0.7-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "45648437e5f731a55262dd859f0a0f4f38fcf58383f7b08c5d9d8d9fab916af7",
"md5": "f93c38ef0a93da9d4ae486cb5d0847d3",
"sha256": "b1d4e2522b90d85e8bd92c8a25b701eb2651ef7548168e7caf2b4e8346455dc5"
},
"downloads": -1,
"filename": "python-readenv-0.7.tar.gz",
"has_sig": false,
"md5_digest": "f93c38ef0a93da9d4ae486cb5d0847d3",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.8",
"size": 8796,
"upload_time": "2024-02-22T13:24:55",
"upload_time_iso_8601": "2024-02-22T13:24:55.878239Z",
"url": "https://files.pythonhosted.org/packages/45/64/8437e5f731a55262dd859f0a0f4f38fcf58383f7b08c5d9d8d9fab916af7/python-readenv-0.7.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-02-22 13:24:55",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "rsalmaso",
"github_project": "python-readenv",
"travis_ci": false,
"coveralls": false,
"github_actions": false,
"lcname": "python-readenv"
}