Skip to main content

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 KeyError exception. You can provide a default value by passing default argument to get_env function.
  • Environment present in configuration files takes precedence over environment variables present in the system.
  • No multiline support for environment variables.

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

newenvreader-0.2.0.tar.gz (4.1 kB view details)

Uploaded Source

Built Distribution

newenvreader-0.2.0-py3-none-any.whl (4.7 kB view details)

Uploaded Python 3

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

Hashes for newenvreader-0.2.0.tar.gz
Algorithm Hash digest
SHA256 e3817163c5fbb3c29e049f587d276147f600122e41245160f480a458421be2c0
MD5 b6c1f3fc52b947ad612ceebbf8e84963
BLAKE2b-256 6c97175d1818b549b4103ee612f5fa2a6ed92c4875945de4dc03c2772d55c2b6

See more details on using hashes here.

File details

Details for the file newenvreader-0.2.0-py3-none-any.whl.

File metadata

File hashes

Hashes for newenvreader-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 9e8521c58231105f7b8da00e1d27baf811049a3af0f466f6cdbdf58a26524b22
MD5 59dfc3fe7cd61b51294f7709d0055636
BLAKE2b-256 a573221dbe24854b7f18c1b93f8d840858048976f631274c37fee414a711170f

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page