Skip to main content

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 (first character [#] on a line)
  • 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.

Metadata

Release files for pyenv-load 0.1.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pyenv-load 0.1.1
File Size Uploaded
pyenv_load-0.1.1.tar.gz 4.4 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for pyenv-load 0.1.1
File Interpreter ABI Platform
pyenv_load-0.1.1-py3-none-any.whl Python 3 none any Details

Total release size: 9.4 kB

Release files / pyenv_load-0.1.1.tar.gz

Download URL pyenv_load-0.1.1.tar.gz
Size 4.4 kB
Tags Source
SHA-256 checksum
How to use checksums
4497d5393e25297fa197388d5739fca47f506bfa98bb12a2732690532f47e4a9
BLAKE2b-256 checksum
How to use checksums
011f63e8782df39b2a0600818ebb8448529615eb3d1fe9d144a05d22d8e3cfc2
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/2.1.1 CPython/3.11.11 Linux/6.8.0-52-generic

Release files / pyenv_load-0.1.1-py3-none-any.whl

Download URL pyenv_load-0.1.1-py3-none-any.whl
Size 5.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
be27437da1c26e23ce0a7b520ec50039299c931de230403b93418d2af839ba86
BLAKE2b-256 checksum
How to use checksums
f86e559c658c0294a8304b5dc537e454a94eccd0704002922ba518612784c061
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/2.1.1 CPython/3.11.11 Linux/6.8.0-52-generic

Release history Release notifications | RSS feed

This release

0.1.1 This release

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page