Skip to main content

Reads the variables from .env files and adds them to environment, also automatically guesses and parses to correct types in Python.

Project description

python-env-loader

Reads the variables from .env files in similar way that is used in React.js and adds them to environment variables, automatically guessing and parsing to correct types in Python. It is great for managing app settings during development, tests and in production without writing additional configuration, and to hold settings separated by environment type clearly in a one place.

Usage

Call load_env on the application start, and it will load environment variables from a certain set of .env files in the selected directory and return and EnvFile object. After that, you can get values by using get method of EnvFile object, or by reading it directly from the environment variables (by using os.environ).

Example .env file looks like this:

SECRET_KEY=test-secret-key.123
EXTERNAL_API_URL=api.example.com
EXTERNAL_API_KEY=this-is-external-api-key
DEBUG=true
ALLOWED_HOSTS=127.0.0.1:8000,0.0.0.0:8000,localhost:8000

Getting started

Installation

Install the latest version with:

pip install -U python-env-loader

First steps

Assuming you have created the .env and .env.local files inside your project root directory:

.
├── .env
└── .env.local

And they have the following content:

# .env
DEBUG=false  # maps to False in python
ALLOWED_HOSTS=,  # maps to empty list []
# .env.local
DEBUG=true  # maps to True in python
ALLOWED_HOSTS=127.0.0.1:8000,0.0.0.0:8000,localhost:8000  # maps to list [127.0.0.1:8000, 0.0.0.0:8000, localhost:8000]

Add the following code to your settings module - for example settings.py in Django:

# settings.py
from env_loader import load_env
env = load_env()  # if env files are in your root directory of the project

or:

# settings.py
from env_loader import load_env
env = load_env("/app")  # if your env file is stored in /app directory

Parsed key/value pairs from the .env and .env.local files are now accessible in env object:

# settings.py
env.get("DEBUG")  # returns True, because .env.local file has higher priority
env.get("IS_TEST_ENV", False)  # returns False, because this key is not present in any env file

or you can use them as system environment variable and they can be conveniently accessed via os.getenv(), but then every value is received as a string:

# settings.py
import os
DEBUG = os.getenv("DEBUG")  # returns "true" AS A STRING

Automatic parsing

python-env-loader automatically guesses the Python type of the environment variable. If a variable consists only of digits, it assumes that it's an integer. If a value consists only of a digits and has a one dot, then it's treated as a float.

More custom parsing:

Python value .env value
True true, True, y, yes
False false, False, n, no
None null, None

Empty string is mapped to None.

You can disable automatic parsing by passing auto_parse=False to load_env function:

from env_loader import load_env, EnvTypes
env = load_env(auto_parse=False)

Switching between setups

When you're loading your env settings, you can switch between 4 of a different setups of them: default, development, production and test. By default, default is used, but you can select different setup by passing a value to env_type argument of load_env function:

from env_loader import load_env, EnvTypes
env = load_env(env_type=EnvTypes.DEVELOPMENT_ENV)

Files hierarchy

For each setup, files on the left have more priority than files on the right:

  • default: .env.local, .env
  • development: .env.development.local, .env.development, .env.local, .env
  • production: .env.production.local, .env.production, .env.local, .env
  • test: .env.test.local, .env.test, .env (note .env.local is missing)

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

python-env-loader-0.0.1.tar.gz (5.4 kB view details)

Uploaded Source

Built Distribution

python_env_loader-0.0.1-py2.py3-none-any.whl (17.6 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file python-env-loader-0.0.1.tar.gz.

File metadata

  • Download URL: python-env-loader-0.0.1.tar.gz
  • Upload date:
  • Size: 5.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.42.0 CPython/3.8.1

File hashes

Hashes for python-env-loader-0.0.1.tar.gz
Algorithm Hash digest
SHA256 8b0feb891d9a576c1a3a3747bdc32080a05ee32ee2e8865e97afc069d1afa479
MD5 d681d1927a5c66e1adaf270e9f934cf5
BLAKE2b-256 7a330ad104d6e1f0fa0994b1d2b4716888aa0b4971283fe6338a832c29b7f6ed

See more details on using hashes here.

File details

Details for the file python_env_loader-0.0.1-py2.py3-none-any.whl.

File metadata

  • Download URL: python_env_loader-0.0.1-py2.py3-none-any.whl
  • Upload date:
  • Size: 17.6 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.42.0 CPython/3.8.1

File hashes

Hashes for python_env_loader-0.0.1-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 869cb3c14f9e4a66b342cba627c598da6ce854c94d2353995a411cb240478290
MD5 1b152c2c5ac1fd63e03f38e4e0c028a6
BLAKE2b-256 9bae106c9156bb43d8bc4dd7a472ac495ff778b9de9cb22e694e3f5445dc4981

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