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
.envfiles or JSON files - Supports comments in
.envfiles (first character [#] on a line) - Validates environment variable names
- Handles quoted values in
.envfiles - 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:
Trueif the file was loaded successfullyFalseif 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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file pyenv_load-0.1.1.tar.gz.
File metadata
- Download URL: pyenv_load-0.1.1.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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
4497d5393e25297fa197388d5739fca47f506bfa98bb12a2732690532f47e4a9
|
|
| MD5 |
b5823d0f1d18f16c7e5a6c51bed659e6
|
|
| BLAKE2b-256 |
011f63e8782df39b2a0600818ebb8448529615eb3d1fe9d144a05d22d8e3cfc2
|
File details
Details for the file pyenv_load-0.1.1-py3-none-any.whl.
File metadata
- Download URL: pyenv_load-0.1.1-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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
be27437da1c26e23ce0a7b520ec50039299c931de230403b93418d2af839ba86
|
|
| MD5 |
79b6d795d9f545075a629234025cfc6b
|
|
| BLAKE2b-256 |
f86e559c658c0294a8304b5dc537e454a94eccd0704002922ba518612784c061
|