envfileparser
This package is as simple as possible to get variables from .env files or files with a similar structure.
Description
The package has no dependencies and works with files located in the same directory as the file from which the functions of this package were called.
Function
get_env()takes the name of the target variable as a string and an optional second parameter - the path to the file. After that, the function returns the value of the variable also as a string.Function
get_envs()accepts a sequence of variable names as a string, and also an optional parameter-the path to the file. This function returns a list of strings that are the values of the desired variables.Function
get_env_from_file()combines the functionality of get_env and get_envs and also if the parameter with variable names is left empty, the function returns a dictionary with all the variables from the file.By default,
.envis specified as the path. Empty lines and lines without the'='character are skipped. In addition, commenting with a prefix of the'#'character is supported.In the event that the specified variable is missing or the desired file cannot be found, the corresponding exceptions are thrown.
Getting started
Install envfileparser from PyPi with pip: pip install envfileparser
Usage example:
The .env file:
CONST1 = 1
CONST2 = -2
VAR1 = "var1"
VAR2 = 'var2'
VAR3 = var3
Python code:
from envfileparser import get_env, get_envs, get_env_from_file
CONST_ONE = get_env('CONST1') # CONST_ONE = '1'
CONST_TWO = get_env('CONST2', file_path='.env') # CONST_TWO = '-2'
all_variables = get_env_from_file()
# all_variables = {'CONST1: '1', 'CONST2': '-2', 'VAR1':= 'var1', 'VAR2': 'var2', 'VAR3': 'var3'}
c1 = all_variables['CONST1'] # c1 = '1'
c2 = all_variables['CONST2'] # c2 = '-2'
variables = get_envs('VAR1', 'VAR2', 'VAR3') # variables = ['var1', 'var2', 'var3']
v1, v2, v3 = variables[0], variables[1], variables[2]
One more .env file example (support for comments):
# Service token.
API_TOKEN = f3u12yf36f12f418449go3294g238
PORT = 3417
IP = 127.0.0.1 # loopback
USER = 'ADMIN'
PASSWORD="12345678"
If your value contains the "#" character, then be sure to put it in quotation marks - otherwise the parser will take all subsequent characters (including the" # " character) as a comment:
VAR1 = envfile#parser
VAR2 = "envfile#parser"
Python code:
from envfileparser import get_envs
print(*get_envs('VAR1', 'VAR2'), sep='\n')
Output:
envfile
envfile#parser
New version 0.0.8:
- Added new function
get_env_from_file().- Combines the functionality of
get_env()andget_envs(). - If the parameter with variable names is left empty, the function returns a dictionary with all the variables from the file.
get_env_from_file()return empty dictionary if file is empty.
- Combines the functionality of
- Fixed problems with comments issue #6.
- Removing right and left side spaces in values (all values are string).
How to contact the maintainer:
Metadata
Release files for envfileparser 0.0.8
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| envfileparser-0.0.8.tar.gz | 18.7 kB | Without provenance | Details for envfileparser-0.0.8.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| envfileparser-0.0.8-py3-none-any.whl | Python 3 | none | any | Details for envfileparser-0.0.8-py3-none-any.whl |
Total release size: 37.6 kB
Release files / envfileparser-0.0.8.tar.gz
| Download URL | envfileparser-0.0.8.tar.gz |
|---|---|
| Size | 18.7 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
0a5d57c961af5056fa237cb10fc3f973fbcf0b5fee6a0bb6e504d26990023387
|
|
BLAKE2b-256 checksum How to use checksums |
42fdcb6c9d4f62f046779a1e88b35d3d2870f18fb90f39d0f0fe9c209720e479
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.4.2 importlib_metadata/4.6.3 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.0 CPython/3.9.5
|
Release files / envfileparser-0.0.8-py3-none-any.whl
| Download URL | envfileparser-0.0.8-py3-none-any.whl |
|---|---|
| Size | 18.9 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
7510c27d7810649fc449b9446f81e45803d6f561c67ffe842f3b4a1596c49db1
|
|
BLAKE2b-256 checksum How to use checksums |
30cc6acf1ce128adfff5894e446aaa2cd7fd82c36ee29a2d6e16a0b91ba64ffb
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.4.2 importlib_metadata/4.6.3 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.0 CPython/3.9.5
|
Security for envfileparser 0.0.8
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.