Skip to content

Instantly share code, notes, and snippets.

@dotku
Created July 11, 2024 22:00
Show Gist options
  • Save dotku/6b43b6ae32ee085aefd95ae26d56ffd0 to your computer and use it in GitHub Desktop.
Save dotku/6b43b6ae32ee085aefd95ae26d56ffd0 to your computer and use it in GitHub Desktop.
load azd env in python
import subprocess
import os
from pprint import pprint
def load_azd_env_values(env_name=None):
command = ["azd", "env", "get-values"]
if env_name:
command.extend(["--environment", env_name])
try:
result = subprocess.run(command, capture_output=True, text=True, check=True)
# Print the result to debug
# print("Command output:", result.stdout)
if not result.stdout.strip():
raise ValueError("The command returned no output.")
env_values = {}
for line in result.stdout.strip().split('\n'):
if '=' in line:
key, value = line.split('=', 1)
env_values[key] = value.strip('"')
else:
raise ValueError(f"Invalid line in output: {line}")
for key, value in env_values.items():
os.environ[key] = value
return env_values
except subprocess.CalledProcessError as e:
print(f"An error occurred while running azd env get-values: {e}")
return None
# Example usage:
env_values = load_azd_env_values()
print("\nCurrent environment variables:")
current_env_values = {key: os.environ.get(key) for key in env_values}
pprint(current_env_values)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment