Skip to main content

No project description provided

Project description

runenv

CI/CD CI - Test
Package PyPI - Version PyPI - Downloads PyPI - Python Version
Meta linting - Ruff code style - Black types - Mypy License - MIT
Changes CHANGELOG.md

You can use runenv to manage your app settings using 12-factor principles.

The runenv package provides a few things:

  • [cli] Wrapper to run programs with modified environment variables loaded from given .env file.
  • [api] Python API to load variables from .env file into environment

Table of Contents

Installation

To install runenv package along with runenv CLI command run:

pip install runenv

Usage

Fist you have to create .env (this is the default name, you can use any name and have multiple environment files in projects) file in your project.

Example .env file:

BASE_URL=http://127.0.0.1:8000
DATABASE_URI=postgres://postgres:password@localhost/dbname

# line with comment
EMAIL_HOST=smtp.mandrillapp.com
EMAIL_PORT=587                             # inline comment
EMAIL_HOST_USER="double quoted value"
EMAIL_HOST_PASSWORD='single quoted value'
EMAIL_FROM=user@${EMAIL_HOST}              # reuse variable from same file
EMAIL_USE_TLS=1

Python API

load_env(env_file=".env", prefix=None, strip_prefix=True, force=False, search_parent=0)`

This function is loading content of .env file into environment without raising errors if file does not exist.

Options:

  • env_file: optional environment file name; default .env
  • prefix: optional prefix to filter loaded variables; f.e. DJANGO_ will load only DJANGO_* variables
  • strip_prefix: whether strip prefix when loading variables; default True; f.e. DJANGO_SECRET will be loaded as SECRET if prefix=DJANGO_
  • force: whether load .env file again, even if application was started by runenv CLI wrapper (this wrapper already is loading .env file)
  • search_parent: how many parent directories search for .env file; default 0

create_env(env_file=".env", prefix=None, strip_prefix=True)`

This function is only parsing content of .env file and returns it as python dictionary, without changing environment.

Options:

  • env_file: optional environment file name; default .env
  • prefix: optional prefix to filter loaded variables; f.e. DJANGO_ will load only DJANGO_* variables
  • strip_prefix: whether strip prefix when loading variables; default True; f.e. DJANGO_SECRET will be loaded as SECRET if prefix=DJANGO_

CLI

The runenv CLI can be used to run commands wrapped with environment loaded from passed .env files.

$ runenv --help
usage: runenv [-h] [-V] [-v {1,2,3}] [-p PREFIX] [-s] [--dry_run] env_file command

Run program with given environment file loaded

positional arguments:
  env_file              Environment file to load
  command               Command to run with loaded environment

options:
  -h, --help            show this help message and exit
  -V, --version         show program's version number and exit
  -v {1,2,3}, --verbosity {1,2,3}
                        verbosity level, 1 - (ERROR, default), 2 - (INFO) or 3 - (DEBUG)
  -p PREFIX, --prefix PREFIX
                        Load only variables with given prefix
  -s, --strip_prefix    Strip prefix given with --prefix from environment variables names
  --dry_run             Return parsed .env instead of running command

Example usage:

Run ./manage.py serve command with environment loaded from .env.development file.

$ runenv .env.development ./manage.py serve

Options:

  • env_file: environment file name to load
  • --prefix: optional prefix to filter loaded variables; f.e. DJANGO_ will load only DJANGO_* variables
  • --strip-prefix: whether strip prefix when loading variables; default True; f.e. DJANGO_SECRET will be loaded as SECRET if prefix=DJANGO_
  • --dry-run: only display parsed .env result instead of loading environment and running command

Integration

To use load_env with Django or Flask, put the following code in manage.py and wsgi.py files.

from runenv import load_env
load_env()

Similar projects

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

runenv-1.2.1.tar.gz (14.1 kB view hashes)

Uploaded Source

Built Distribution

runenv-1.2.1-py2.py3-none-any.whl (7.2 kB view hashes)

Uploaded Python 2 Python 3

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