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.
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.
pip install dotenv-azd
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
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
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)
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
.
dotenv-azd
is distributed under the terms of the MIT license.