Please remove embedded credentials from Python scripts before emailing them to DataSelf.
Use Python’s
os.getenv
method to replace constants and literal values of credentials and other secure values.Email the Python script to DataSelf.
Sent credentials outside of email (e.g., text).
os.getenv Method
Python’s os.getenv
method returns the value of the environment variable key, if it exists.
(For testing on your computer see Create Windows Environment Variables with PowerShell)
os.getenv Usage
import os # Retrieve the value of the PASSWORD environment variable password_credential = os.getenv('PASSWORD') print(f"Password credential: {password_credential}") # Optional Default if environment variable does not exist # Attempt to retrieve a non-existent environment variable with a default value custom_variable = os.getenv('CUSTOM_VARIABLE', 'default_value') print(f"Custom Variable: {custom_variable}")
Example Script
Code snippets taken from a working Python script
import os body={ "name" : "gshu", "password" : os.getenv('CONNECTED_ACU_API_PASSWORD'), #Modify this to get password "company" : "Connected", "branch" : "2J", "locale" : "en-US" } def get_metrc_encoding(): vendor_key = os.getenv('Acme_ACU_API_VENDOR_KEY') # Modify this to get vendor key user_key = os.getenv('Acme_ACU_API_USER_KEY') # Modify this to get user key encoding_str = f"{vendor_key}:{user_key}" encoding = base64.b64encode(encoding_str.encode()) encoding = encoding.decode('ASCII') return encoding
Naming Convention
DataSelf’s naming convention for environment variables used in scripts is:
<CLIENTNAME>_<SCRIPT_NAME>_<VARIABLENAM>.
For example: Acme_ACU_API_PASSWORD, Acme_ACU_API_VENDOR_KEY, Acme_ACU_API_USER_KEY
Related Pages
https://www.w3schools.com/python/ref_os_getenv.asp Python os.getenv() Method