/dotenv-azd

Python dotenv adapter that loads dotenv key value pairs from Azure Developer CLI (azd)

Primary LanguagePythonMIT LicenseMIT

dotenv-azd

PyPI - Version PyPI - Status PyPI - Python Version PyPI - Downloads

dotenv-azd allows seamless integration of Azure Developer CLI (azd) environment variables into your Python applications without the need to manually export them to an .env file. This can greatly enhance productivity and reduce potential errors, allowing for a smoother development experience.

It leverages the azd CLI and the python-dotenv library.

Why dotenv-azd

Integrating azd environment variables directly into your Python applications can streamline your development process. By avoiding the manual export to .env files, you can reduce overhead and minimize mistakes. This is especially useful when switching between different azd environments.

Installation

pip install dotenv-azd

Usage

Basic Usage

Create a new AZD env if you don't have one yet and set an initial variable value:

azd init MY_AZD_ENV
azd env set VAR1 OVERRIDE

In your Python code:

from dotenv_azd import load_azd_env
from os import getenv, environ

environ['VAR1'] = 'INITIAL'

load_azd_env()

print(getenv('AZURE_ENV_NAME')) # prints 'MY_AZD_ENV', loaded from azd env

print(getenv('VAR1')) # prints 'INITIAL', was already in Python env

Override mode

You can also override variables in Python env:

load_azd_env(override=True)
print(getenv('VAR1')) # prints 'OVERRIDE', loaded from azd env, overriding Python env

Quiet mode

If you want to ignore errors when azd is not initialized or no azd environment is active, you can use the quiet parameter. This is useful when integrating with azd while avoiding dependency on it.

load_azd_env(quiet=True)

Alternatives

The traditional approach to integrate azd environment variables is to export them to a .env file and load that file:

azd env get-values > .env

This approach can create variable quoting issues and might lead to stale variables when switching between environments using azd select.

License

dotenv-azd is distributed under the terms of the MIT license.