# envu
This package provides a convenient way to load environment variables from the `.env` file and system environment variables, automatically parsing them into appropriate Python types (`bool`, `int`, `float`, or `str`). It also allows you to access the loaded variables as attributes.
**Features:**
- Parses `.env` file values into their correct types.
- Supports boolean, integer, float, and string conversions.
- Skips empty lines and comments.
- Raises an error for malformed `.env` lines or missing variables.
- Provides a `__dir__` method for inspecting available keys.
## Installation
```sh
pip install envu
```
## Usage
```python
>>> import env # not envu
>>> env.KEY # Access environment variables as attributes
'Value'
>>> env.DEBUG # Boolean Variable
False
>>> env.SECRET_KEY # String Variable
'fake-secret-key'
>>> env.POSTGRESQL_PORT # Integer Variable
5432
>>> env.SLEEP_TIME # Float Variable
1.5
>>> env.ALLOWED_HOSTS.split(",") # Convert to a list
["localhost", "127.0.0.1", "example.com"]
>>> env.UNDEFINED_VARIABLE # Undefined Variable
Traceback (most recent call last):
...
AttributeError: Environment variable 'UNDEFINED_VARIABLE' not found.
>>> hasattr(env, "KEY") # Check if an environment variable exists
True
>>> getattr(env, "MY_VARIABLE", "default_value") # Get the variable with a default fallback
"default_value"
```
### Example: How do I use it with Django?
For a typical Django project, the `.env` file is placed like this:
```bash
my_project/
├── .env # Your environment variables
├── manage.py # Django project entry point
├── my_project/ # Main application folder
│ ├── __init__.py
│ ├── settings.py # Django settings file where .env is loaded
│ ├── urls.py
│ └── wsgi.py
└── apps/
├── app1/
└── app2/
```
An example of Django settings
```python
import env
from unipath import Path
BASE_DIR = Path(__file__).parent
SECRET_KEY = env.SECRET_KEY
DEBUG = getattr(env, "DEBUG", False)
ALLOWED_HOSTS = env.ALLOWED_HOSTS.split(",")
DATABASES = {
"default": {
"ENGINE": "django.db.backends.postgresql",
"NAME": env.DJANGO_DB_NAME,
"USER": env.DJANGO_DB_USERNAME,
"PASSWORD": env.DJANGO_DB_PASSWORD,
"PORT": getattr(env, "DJANGO_DB_PORT", 5432),
"HOST": getattr(env, "DJANGO_DB_HOST", "localhost"),
}
}
EMAIL_HOST_PASSWORD = env.EMAIL_HOST_PASSWORD
EMAIL_HOST_USER = env.EMAIL_HOST_USER
EMAIL_PORT = getattr(env, "EMAIL_PORT", 25)
EMAIL_HOST = getattr(env, "EMAIL_HOST", "localhost")
EMAIL_USE_TLS = getattr(env, "EMAIL_USE_TLS", False)
# ...
```
## Running Tests
To run the tests, make sure you have `pytest` installed. You can install it using `requirements.txt`:
```sh
pip install -r requirements.txt
```
Then run:
```sh
pytest
```
This command will discover and execute all test cases in the project.
For more advanced options, you can refer to the [pytest documentation](https://docs.pytest.org/en/stable/).
Raw data
{
"_id": null,
"home_page": "https://github.com/hashem-nowruzi/envu",
"name": "envu",
"maintainer": "Mohammad Hashem Nowruzi",
"docs_url": null,
"requires_python": "<4.0,>=3.8",
"maintainer_email": "hashem.nowruzi@outlook.com",
"keywords": "env, .env, environment, environment variables",
"author": "Mohammad Hashem Nowruzi",
"author_email": "hashem.nowruzi@outlook.com",
"download_url": "https://files.pythonhosted.org/packages/0a/87/e96626b5100c5208fcaa3e47910aa678482901f511744131551b008c4f93/envu-1.1.0.tar.gz",
"platform": null,
"description": "# envu\n\nThis package provides a convenient way to load environment variables from the `.env` file and system environment variables, automatically parsing them into appropriate Python types (`bool`, `int`, `float`, or `str`). It also allows you to access the loaded variables as attributes.\n\n**Features:**\n\n- Parses `.env` file values into their correct types.\n- Supports boolean, integer, float, and string conversions.\n- Skips empty lines and comments.\n- Raises an error for malformed `.env` lines or missing variables.\n- Provides a `__dir__` method for inspecting available keys.\n\n## Installation\n\n```sh\npip install envu\n```\n\n## Usage\n\n```python\n>>> import env # not envu\n\n>>> env.KEY # Access environment variables as attributes\n'Value'\n\n>>> env.DEBUG # Boolean Variable\nFalse\n\n>>> env.SECRET_KEY # String Variable\n'fake-secret-key'\n\n>>> env.POSTGRESQL_PORT # Integer Variable\n5432\n\n>>> env.SLEEP_TIME # Float Variable\n1.5\n\n>>> env.ALLOWED_HOSTS.split(\",\") # Convert to a list\n[\"localhost\", \"127.0.0.1\", \"example.com\"]\n\n>>> env.UNDEFINED_VARIABLE # Undefined Variable\nTraceback (most recent call last):\n ...\nAttributeError: Environment variable 'UNDEFINED_VARIABLE' not found.\n\n>>> hasattr(env, \"KEY\") # Check if an environment variable exists\nTrue\n\n>>> getattr(env, \"MY_VARIABLE\", \"default_value\") # Get the variable with a default fallback\n\"default_value\"\n\n```\n\n### Example: How do I use it with Django?\n\nFor a typical Django project, the `.env` file is placed like this:\n\n```bash\nmy_project/\n\u251c\u2500\u2500 .env # Your environment variables\n\u251c\u2500\u2500 manage.py # Django project entry point\n\u251c\u2500\u2500 my_project/ # Main application folder\n\u2502 \u251c\u2500\u2500 __init__.py\n\u2502 \u251c\u2500\u2500 settings.py # Django settings file where .env is loaded\n\u2502 \u251c\u2500\u2500 urls.py\n\u2502 \u2514\u2500\u2500 wsgi.py\n\u2514\u2500\u2500 apps/\n \u251c\u2500\u2500 app1/\n \u2514\u2500\u2500 app2/\n```\n\nAn example of Django settings\n\n```python\nimport env\nfrom unipath import Path\n\nBASE_DIR = Path(__file__).parent\n\n\nSECRET_KEY = env.SECRET_KEY\nDEBUG = getattr(env, \"DEBUG\", False)\nALLOWED_HOSTS = env.ALLOWED_HOSTS.split(\",\")\n\n\nDATABASES = {\n \"default\": {\n \"ENGINE\": \"django.db.backends.postgresql\",\n \"NAME\": env.DJANGO_DB_NAME,\n \"USER\": env.DJANGO_DB_USERNAME,\n \"PASSWORD\": env.DJANGO_DB_PASSWORD,\n \"PORT\": getattr(env, \"DJANGO_DB_PORT\", 5432),\n \"HOST\": getattr(env, \"DJANGO_DB_HOST\", \"localhost\"),\n }\n}\n\n\nEMAIL_HOST_PASSWORD = env.EMAIL_HOST_PASSWORD\nEMAIL_HOST_USER = env.EMAIL_HOST_USER\nEMAIL_PORT = getattr(env, \"EMAIL_PORT\", 25)\nEMAIL_HOST = getattr(env, \"EMAIL_HOST\", \"localhost\")\nEMAIL_USE_TLS = getattr(env, \"EMAIL_USE_TLS\", False)\n\n# ...\n```\n\n## Running Tests\n\nTo run the tests, make sure you have `pytest` installed. You can install it using `requirements.txt`:\n\n```sh\npip install -r requirements.txt\n```\n\nThen run:\n\n```sh\npytest\n```\n\nThis command will discover and execute all test cases in the project.\nFor more advanced options, you can refer to the [pytest documentation](https://docs.pytest.org/en/stable/).\n",
"bugtrack_url": null,
"license": "MIT",
"summary": "The simplest possible syntax for loading environment variables from the .env file and system environment variables.",
"version": "1.1.0",
"project_urls": {
"Bug Tracker": "https://github.com/hashem-nowruzi/envu/issues",
"Homepage": "https://github.com/hashem-nowruzi/envu",
"Repository": "https://github.com/hashem-nowruzi/envu"
},
"split_keywords": [
"env",
" .env",
" environment",
" environment variables"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "0f435b35711054bea094cac6561c5dcfd7d391e03a028e39853be76d830f5015",
"md5": "2189d3006261d0a0e9f48d4b7804f532",
"sha256": "1f0f0cdcd446551201f94aecf3486d471430c39a817bee9d7bfb6b4cedd7fac9"
},
"downloads": -1,
"filename": "envu-1.1.0-py3-none-any.whl",
"has_sig": false,
"md5_digest": "2189d3006261d0a0e9f48d4b7804f532",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": "<4.0,>=3.8",
"size": 4034,
"upload_time": "2025-02-19T17:29:25",
"upload_time_iso_8601": "2025-02-19T17:29:25.896307Z",
"url": "https://files.pythonhosted.org/packages/0f/43/5b35711054bea094cac6561c5dcfd7d391e03a028e39853be76d830f5015/envu-1.1.0-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "0a87e96626b5100c5208fcaa3e47910aa678482901f511744131551b008c4f93",
"md5": "d5cb0ba1887da3d0f8427bb02af18f13",
"sha256": "7eb2b88b29cebc146c2e694cb40b3963ff7a20fd4c9c64d04bd2dcf9c6c44f12"
},
"downloads": -1,
"filename": "envu-1.1.0.tar.gz",
"has_sig": false,
"md5_digest": "d5cb0ba1887da3d0f8427bb02af18f13",
"packagetype": "sdist",
"python_version": "source",
"requires_python": "<4.0,>=3.8",
"size": 3543,
"upload_time": "2025-02-19T17:29:30",
"upload_time_iso_8601": "2025-02-19T17:29:30.497875Z",
"url": "https://files.pythonhosted.org/packages/0a/87/e96626b5100c5208fcaa3e47910aa678482901f511744131551b008c4f93/envu-1.1.0.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2025-02-19 17:29:30",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "hashem-nowruzi",
"github_project": "envu",
"travis_ci": false,
"coveralls": false,
"github_actions": false,
"requirements": [
{
"name": "pytest",
"specs": [
[
"==",
"8.3.4"
]
]
},
{
"name": "ipython",
"specs": [
[
"==",
"8.31.0"
]
]
}
],
"lcname": "envu"
}