core

dotenv_to_edotenv

edotenv.core.dotenv_to_edotenv(dotenv_path='.env', edotenv_path='.env', key_path=None, *args, **kwargs)[source]

Encrypt a .env file.

Parameters
  • dotenv_path (str) – The path of the .env file.

  • edotenv_path (str) – The path of the encrypted .env file.

  • key_path (str or None) –

    The path to the key used to encrypt and decrypt the .env file.

    • If the file does not exist, then a key file will be automatically generated

    • If None, defaults to a file inside the package’s directory

  • *args – Additional arguments passed to dotenv.dotenv_values.

  • **kwargs

    Additional arguments passed to dotenv.dotenv_values.

Example

import tempfile
import os

from edotenv import dotenv_to_edotenv, load_edotenv

with tempfile.TemporaryDirectory() as folder:

    # Remove vars for testing
    if 'TESTINGA' in os.environ:
        del os.environ['TESTINGA']
    if 'TESTINGB' in os.environ:
        del os.environ['TESTINGB']

    # Create a .env file with vars TESTINGA and TESTINGB
    dotenv_path = f'{folder}/.env'
    with open(dotenv_path, 'w') as dotenv_file:
        dotenv_file.write('TESTINGA=testinga123\nTESTINGB=testingb123')

    # Check if the vars exist
    print('TESTINGA in env (not loaded): ' + str('TESTINGA' in os.environ))
    print('TESTINGB in env (not loaded): ' + str('TESTINGA' in os.environ))

    # Encrypt the .env file
    edotenv_path = f'{folder}/.env.encrypted'
    key_path = f'{folder}/.env.key'
    dotenv_to_edotenv(dotenv_path, edotenv_path, key_path)

    # Load the encrypted .env file
    load_edotenv(edotenv_path, key_path)

    # Check if vars exist again
    print('TESTINGA value (loaded): ' + str(os.environ['TESTINGA']))
    print('TESTINGB value (loaded): ' + str(os.environ['TESTINGB']))
TESTINGA in env (not loaded): False
TESTINGB in env (not loaded): False
TESTINGA value (loaded): testinga123
TESTINGB value (loaded): testingb123

edotenv_to_dotenv

edotenv.core.edotenv_to_dotenv(dotenv_path='.env', edotenv_path='.env', key_path=None, *args, **kwargs)[source]

Decrypt a .env file.

Parameters
  • dotenv_path (str) – The path of the .env file.

  • edotenv_path (str) – The path of the encrypted .env file.

  • key_path (str or None) –

    The path to the key used to encrypt and decrypt the .env file.

    • If the file does not exist, then a key file will be automatically generated

    • If None, defaults to a file inside the package’s directory

Example

import tempfile
import os

from dotenv import load_dotenv
from edotenv import dotenv_to_edotenv, load_edotenv, edotenv_to_dotenv

with tempfile.TemporaryDirectory() as folder:

    # Remove vars for testing
    if 'TESTINGA' in os.environ:
        del os.environ['TESTINGA']
    if 'TESTINGB' in os.environ:
        del os.environ['TESTINGB']

    # Create a .env file with vars TESTINGA and TESTINGB
    dotenv_path = f'{folder}/.env'
    with open(dotenv_path, 'w') as dotenv_file:
        dotenv_file.write('TESTINGA=testinga123\nTESTINGB=testingb123')

    # Check if the vars exist
    print('TESTINGA in env (not loaded): ' + str('TESTINGA' in os.environ))
    print('TESTINGB in env (not loaded): ' + str('TESTINGA' in os.environ))

    # Encrypt the .env file
    edotenv_path = f'{folder}/.env.encrypted'
    key_path = f'{folder}/.env.key'
    dotenv_to_edotenv(dotenv_path, edotenv_path, key_path)

    # Load the encrypted .env file
    load_edotenv(edotenv_path, key_path)

    # Check if vars exist again
    print('TESTINGA value (loaded): ' + str(os.environ['TESTINGA']))
    print('TESTINGB value (loaded): ' + str(os.environ['TESTINGB']))

    # Decrypt the .env file
    dotenv_path = f'{folder}/.env.decrypted'
    edotenv_to_dotenv(dotenv_path, edotenv_path, key_path)

    # Remove vars for testing
    if 'TESTINGA' in os.environ:
        del os.environ['TESTINGA']
    if 'TESTINGB' in os.environ:
        del os.environ['TESTINGB']

    # Check if the vars exist after removal for testing decrypted file
    print('TESTINGA in env (before loading decrypt): ' + str('TESTINGA' in os.environ))
    print('TESTINGB in env (before loading decrypt): ' + str('TESTINGA' in os.environ))

    # Load the decrypted .env file
    load_dotenv(dotenv_path)

    # Check if vars exist again after loading decrypted file
    print('TESTINGA value (after loading decrypt): ' + str(os.environ['TESTINGA']))
    print('TESTINGB value (after loading decrypt): ' + str(os.environ['TESTINGB']))
TESTINGA in env (not loaded): False
TESTINGB in env (not loaded): False
TESTINGA value (loaded): testinga123
TESTINGB value (loaded): testingb123
TESTINGA in env (before loading decrypt): False
TESTINGB in env (before loading decrypt): False
TESTINGA value (after loading decrypt): testinga123
TESTINGB value (after loading decrypt): testingb123

load_edotenv

edotenv.core.load_edotenv(edotenv_path='.env', key_path=None, *args, **kwargs)[source]

Load environmental varables from an encrypted .env file.

Parameters
  • edotenv_path (str) – The path of the encrypted .env file.

  • key_path (str or None) – The path to the key used to encrypt and decrypt the .env file. If None, defaults to a file inside the package’s directory.

  • *args – Additional arguments passed to dotenv.load_dotenv.

  • **kwargs

    Additional arguments passed to dotenv.load_dotenv.

Example

import tempfile
import os

from edotenv import dotenv_to_edotenv, load_edotenv

with tempfile.TemporaryDirectory() as folder:

    # Remove vars for testing
    if 'TESTINGA' in os.environ:
        del os.environ['TESTINGA']
    if 'TESTINGB' in os.environ:
        del os.environ['TESTINGB']

    # Create a .env file with vars TESTINGA and TESTINGB
    dotenv_path = f'{folder}/.env'
    with open(dotenv_path, 'w') as dotenv_file:
        dotenv_file.write('TESTINGA=testinga123\nTESTINGB=testingb123')

    # Check if the vars exist
    print('TESTINGA in env (not loaded): ' + str('TESTINGA' in os.environ))
    print('TESTINGB in env (not loaded): ' + str('TESTINGA' in os.environ))

    # Encrypt the .env file
    edotenv_path = f'{folder}/.env.encrypted'
    key_path = f'{folder}/.env.key'
    dotenv_to_edotenv(dotenv_path, edotenv_path, key_path)

    # Load the encrypted .env file
    load_edotenv(edotenv_path, key_path)

    # Check if vars exist again
    print('TESTINGA value (loaded): ' + str(os.environ['TESTINGA']))
    print('TESTINGB value (loaded): ' + str(os.environ['TESTINGB']))
TESTINGA in env (not loaded): False
TESTINGB in env (not loaded): False
TESTINGA value (loaded): testinga123
TESTINGB value (loaded): testingb123

save_edotenv

edotenv.core.save_edotenv(vars, edotenv_path='.env', key_path=None)[source]

Load environmental varables from an encrypted .env file.

Parameters
  • edotenv_path (str) – The path of the encrypted .env file.

  • key_path (str or None) –

    The path to the key used to encrypt and decrypt the .env file.

    • If the file does not exist, then a key file will be automatically generated

    • If None, defaults to a file inside the package’s directory

  • vars (str OR list) – A list of the environmental variable names to save into the encrypted .env file.

Example

import tempfile
import os

from edotenv import save_edotenv, load_edotenv

with tempfile.TemporaryDirectory() as folder:

    # Remove vars for testing
    if 'TESTINGA' in os.environ:
        del os.environ['TESTINGA']
    if 'TESTINGB' in os.environ:
        del os.environ['TESTINGB']

    # Set env vars TESTINGA and TESTINGB
    os.environ['TESTINGA'] = 'testinga123'
    os.environ['TESTINGB'] = 'testingb123'

    # Check the values of the vars
    print('TESTINGA value (before save): ' + str(os.environ['TESTINGA']))
    print('TESTINGB value (before save): ' + str(os.environ['TESTINGB']))

    # Save an encrypted .env file of the vars
    edotenv_path = f'{folder}/.env.encrypted'
    key_path = f'{folder}/.env.key'
    vars = ['TESTINGA', 'TESTINGB']
    save_edotenv(vars, edotenv_path, key_path)

    # Load the encrypted .env file
    load_edotenv(edotenv_path, key_path)

    # Check if the vars loaded correctly from encrypted .env file
    print('TESTINGA value (after save): ' + str(os.environ['TESTINGA']))
    print('TESTINGB value (after save): ' + str(os.environ['TESTINGB']))
TESTINGA value (before save): testinga123
TESTINGB value (before save): testingb123
TESTINGA value (after save): testinga123
TESTINGB value (after save): testingb123