Skip to main content

Loads from text or JSON file into environment

Project description

pyenv-load

A simple Python package to load environment variables from .env files or JSON files.

Installation

You can install the package from PyPI:

pip install pyenv-load

Or using Poetry:

poetry add pyenv-load

Usage

Loading from a .env file

Create a .env.local file in your project directory:

DEBUG=True
API_KEY=my-secret-key
DB_HOST=localhost
DB_PORT=5432

Then, in your Python code:

from pyenv_load import load_env

# Load from default .env.local in the current script's directory
load_env()

# Or specify a custom file and path
load_env(filename=".env.production", path="/path/to/config")

# Access the environment variables
import os
debug = os.environ.get("DEBUG")
api_key = os.environ.get("API_KEY")

Loading from a JSON file

Create a .env.json file in your project directory:

{
  "DEBUG": true,
  "API_KEY": "my-secret-key",
  "DB_HOST": "localhost",
  "DB_PORT": 5432
}

Then, in your Python code:

from pyenv_load import load_env

# Load from a JSON file
load_env(filename=".env.json")

# Access the environment variables
import os
debug = os.environ.get("DEBUG")
api_key = os.environ.get("API_KEY")

Features

  • Load environment variables from .env files or JSON files
  • Supports comments in .env files
  • Validates environment variable names
  • Handles quoted values in .env files
  • Converts all values to strings when setting environment variables
  • Properly formats complex JSON types (lists and dictionaries) using JSON serialization
  • Automatic detection of file type based on file extension

JSON Value Handling

When loading from JSON files:

  • Simple values (strings, numbers, booleans, null) are converted using str()
  • Complex values (arrays, objects) are converted using json.dumps()

For example, this JSON:

{
  "STRING_KEY": "value",
  "NUMBER_KEY": 42,
  "ARRAY_KEY": [1, 2, 3],
  "OBJECT_KEY": {"nested": "value"}
}

Will produce these environment variables:

STRING_KEY=value
NUMBER_KEY=42
ARRAY_KEY=[1, 2, 3]
OBJECT_KEY={"nested": "value"}

Environment Variable Rules

  • Names must start with a letter or underscore
  • Names can only contain letters, numbers, and underscores
  • All values are converted to strings in the environment

Error Handling

The load_env function returns:

  • True if the file was loaded successfully
  • False if an error occurred (with an error message printed to console)

License

This project is licensed under the MIT License - see the LICENSE file for details.

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

pyenv_load-0.1.0.tar.gz (4.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pyenv_load-0.1.0-py3-none-any.whl (5.0 kB view details)

Uploaded Python 3

File details

Details for the file pyenv_load-0.1.0.tar.gz.

File metadata

  • Download URL: pyenv_load-0.1.0.tar.gz
  • Upload date:
  • Size: 4.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.1.1 CPython/3.11.11 Linux/6.8.0-52-generic

File hashes

Hashes for pyenv_load-0.1.0.tar.gz
Algorithm Hash digest
SHA256 dae0b83741c55de673f30d287bc606f3b0417212f5fc558cbf610fdc5fd27575
MD5 680efe37e4ec8ec7d0b6ab156904c2be
BLAKE2b-256 8207c378bf1751d10f923ba0ec02482df812900ca2344b3ce111a45ad544cbb2

See more details on using hashes here.

File details

Details for the file pyenv_load-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: pyenv_load-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 5.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.1.1 CPython/3.11.11 Linux/6.8.0-52-generic

File hashes

Hashes for pyenv_load-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 1b0d8dc4f1befd2dddc5b08ec8f01e06739212f599329f8a4dff90232241c626
MD5 b4daf91361fe0d67387b9ba2a5fa216b
BLAKE2b-256 2f4d05bab03bbcb49b1c9596c206896b56fab00870880718624fdedddd20b397

See more details on using hashes here.

Supported by

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