No project description provided
Project description
New Env Reader
A python library that provides an easy way to load environment variables from a .env or .ini file along with type hint support in your IDE.
Motivation
Since the introduction of type hints in python, it is possible for IDEs to provide type checking. Type hints is very useful in improving developer experience and preventing possible bugs due to type mismatch. However, existing libraries for reading environment variables does not provide exact type hint support. This library aims to provide a easy way to read environment variables with type hint support.
Credits
This library takes inspiration from a great library python-decouple.
Installation
pip install newenvreader
Usage
It is recommended to store configuration in the environment separate from code as per 12 Factor App methodology. So, let's start by creating a configuration file in the root of your project. This library supports both .env and .ini file formats.
Env file
Create a .env file in the root of your project. This file will contain all your environment variables.
STR_ENV=hello
INT_ENV=123
FLOAT_ENV=123.456
DB_URL=postgres://user:password@localhost:5432/dbname
Ini file
Incase you prefer .ini file over .env file, you can create a settings.ini file in the root of your project. This file will contain all your environment variables.
[settings]
STR_ENV=hello
INT_ENV=123
FLOAT_ENV=123.456
DB_URL=postgres://user:password@localhost:5432/dbname
Loading environment variables
Import the module in one single place in your application. This will automatically load the environment variables from .env file. Then use get_env function to read environment variables.
from newenvreader import get_env
val = get_env("STR_ENV")
print(val) # "hello"
# Parse as int
val = get_env("INT_ENV", cast=int)
print(val) # 123
# Provide a default value if environment variable is not set
val = get_env("MY_VAR", default="default value")
print(val) # "default value"
Hint: It is recommended to use get_env function in one single place to get all your environment variables as constants and then import these constants anywhere you need them.
Casting
You cast the environment variable to any type you want. All available types in python are supported.
from newenvreader import get_env
# Cast as int
val = get_env("INT_ENV", cast=int)
print(val) # 123
# Cast as float
val = get_env("FLOAT_ENV", cast=float)
print(val) # 123.456
# Cast as bool
val = get_env("BOOL_ENV", cast=bool)
print(val) # True
When casting as boolean, the following values are considered as True: true, t, yes, y, 1, on. And similarly for False: false, f, no, n, 0, "".
You can also cast to your custom types. For example, if you have a custom type MyType, you can cast to it like this:
from newenvreader import get_env
val = get_env("MY_VAR", cast=MyType)
Note: when casting int and float types incase the environment variable is not a valid number, it will raise a ValueError.
Caveats
- Undefined environment variables will raise a
KeyErrorexception. You can provide a default value by passingdefaultargument toget_envfunction. - Environment present in configuration files takes precedence over environment variables present in the system.
- No multiline support for environment variables.
Project details
Release history Release notifications | RSS feed
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
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file newenvreader-0.2.0.tar.gz.
File metadata
- Download URL: newenvreader-0.2.0.tar.gz
- Upload date:
- Size: 4.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/4.0.1 CPython/3.11.4
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e3817163c5fbb3c29e049f587d276147f600122e41245160f480a458421be2c0
|
|
| MD5 |
b6c1f3fc52b947ad612ceebbf8e84963
|
|
| BLAKE2b-256 |
6c97175d1818b549b4103ee612f5fa2a6ed92c4875945de4dc03c2772d55c2b6
|
File details
Details for the file newenvreader-0.2.0-py3-none-any.whl.
File metadata
- Download URL: newenvreader-0.2.0-py3-none-any.whl
- Upload date:
- Size: 4.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/4.0.1 CPython/3.11.4
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
9e8521c58231105f7b8da00e1d27baf811049a3af0f466f6cdbdf58a26524b22
|
|
| MD5 |
59dfc3fe7cd61b51294f7709d0055636
|
|
| BLAKE2b-256 |
a573221dbe24854b7f18c1b93f8d840858048976f631274c37fee414a711170f
|