Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Azure Python SDK - code only fails when debugging in VS Code

VS Code 1.14.0, Python 3.8.0 (in venv).

When I run the following code in VS Code, it works. When I run it in the debugger even with no breakpoints, it fails. This might be something to do with venvs, but I don't know. Ideas? BTW - I am referencing the other packages for what I will be building.

From the Bash shell, I have the following environment variables:

    export AZURE_TENANT_ID = "tenant ID"
    export AZURE_CLIENT_ID = "client ID"
    export AZURE_CLIENT_SECRET = "client secret"
    export AZURE_SUBSCRIPTION_ID = "subscription ID"
from azure.identity import DefaultAzureCredential
from azure.keyvault.secrets import SecretClient
from azure.keyvault.keys import KeyClient
from azure.mgmt.keyvault import KeyVaultManagementClient
from azure.core.exceptions import HttpResponseError
import datetime
import os

credential = DefaultAzureCredential()
secret_client = SecretClient(vault_url="https://blahblahblah.vault.azure.net/", credential=credential)

print(os.environ["AZURE_TENANT_ID"])
print(os.environ["AZURE_CLIENT_ID"])
print(os.environ["AZURE_CLIENT_SECRET"])
print(os.environ["AZURE_SUBSCRIPTION_ID"])

try:
    print("\n.. Get a Secret by name")
    secret = secret_client.get_secret("mySecret")
    print("Secret with name '{0}' was found with value '{1}'.".format(secret.name, secret.value))


except HttpResponseError as e:
    print("\nThis sample has caught an error. {0}".format(e.message))

When I run this in DEBUG in VS Code, this is the error:

This sample has caught an error.
No credential in this chain provided a token.
Attempted credentials:
        EnvironmentCredential: Incomplete environment configuration
        ImdsCredential: IMDS endpoint unavailable
        SharedTokenCacheCredential: The shared cache contains no signed-in accounts. To authenticate with SharedTokenCacheCredential, login
through developer tooling supporting Azure single sign on

What I have learned is the printed OS environ variables are accurate when I Run Python File in Terminal, but when I run the file in debug, it errors printing the first OS environ variable saying it doesn't exist.

This is my ignorance on setting debug correctly. Any pointers will help (and thank you for your responses)!**

like image 979
PJ Johnson Avatar asked Jun 24 '26 01:06

PJ Johnson


2 Answers

To make the answer visible to others, I'm summarizing the answer shared in comment:

This issue occurred because of the missing of environment variables under debugger mode. Add the environment variables to the launch.json file solved this issue.

like image 77
Tony Ju Avatar answered Jun 25 '26 15:06

Tony Ju


From my experience, BASH does not like spaces, when declaring variables;

export AZURE_TENANT_ID="tenant ID"
export AZURE_CLIENT_ID="client ID"
export AZURE_CLIENT_SECRET="client secret"
export AZURE_SUBSCRIPTION_ID="subscription ID"

Might do the trick.

like image 22
cLupus Avatar answered Jun 25 '26 14:06

cLupus