Python3 Environment Variables Example using DOTENV Package
Populate Constants from Environment Variables Code
# constants.py
import os
from cryptography.fernet import Fernet
from dotenv import load_dotenv
# Load environment variables from .env file
load_dotenv()
# Retrieve the decryption key and encrypted password from environment variables
DECRYPTION_KEY = bytes(os.getenv("DECRYPTION_KEY"), encoding='utf-8')
ENCRYPTED_PASSWORD = bytes(os.getenv("ENCRYPTED_PASSWORD"), encoding='utf-8')
# Create a cipher suite using the decryption key
cipher_suite = Fernet(DECRYPTION_KEY)
# Decrypt the password using the cipher suite and convert it to a string
password = cipher_suite.decrypt(ENCRYPTED_PASSWORD).decode()
# Print the decrypted password
print("Password: ", password)
.env Environment Variables File
# .env
DECRYPTION_KEY="G5YLK1KdKpQZjT5p30Chd9SJ2KVc9e_7qeQ1EDufPHU="
ENCRYPTED_PASSWORD="gAAAAABkw6rzPNKDivFJgvBMkY_07i2MI0ZOmXMw-oJgTPXzLS8p7SmJ6O7uDiL652s8AjHc5kEoL6KX3Wy-gp4cSgfdsted3g=="
Example Output
Decrypted password: Pa$$w0rd123!
Process finished with exit code 0
0 Comments Received
Leave A Reply