Skip to content

stefanlight8/dotenv

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

dotenv

A minimal module for working with dotenv files.

Examples

>>> from dotenv import load_dotenv, getenv
>>> load_dotenv()

Load the dotenv file from .env. If any error occurs, nothing will happen. However, if the value is being retrieved from a load_dotenv() call:

>>> env = load_dotenv()
>>> print(env)  # `None` if all is alright.
>>> print(env)  # In case of `Exception`, Houston, we have a problem.

>>> load_dotenv(verbose=True)

In this case, if there's an exception, the function will raise it.

>>> key: str = getenv("API_KEY")

getenv() returns the value from the environment by the key if it exists. If the key doesn't exist, getenv() will raise a KeyError.

To avoid this exception:

>>> max_connections: Union[str, int] = getenv("MAX_CONNECTIONS", default=100)

You can pass any value as the default argument, which prevents the exception!

However, as you can see, max_connections might be str, which could be problematic. To avoid this, getenv() provides a way to cast the result:

>>> max_connections: int = getenv("MAX_CONNECTIONS", into=int)

Now max_connections will be of type int.

Installation

> pip install git+https://github.com/stefanlight8/dotenv

Requirements

  • Python 3.6<

About

A minimal module for working with dotenv files.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Languages