简体   繁体   English

Azure Python SDK - 代码仅在 VS Code 中调试时失败

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

VS Code 1.14.0, Python 3.8.0 (in venv). VS Code 1.14.0,Python 3.8.0(在 venv 中)。

When I run the following code in VS Code, it works.当我在 VS Code 中运行以下代码时,它可以工作。 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.这可能与 venvs 有关,但我不知道。 Ideas?想法? BTW - I am referencing the other packages for what I will be building.顺便说一句 - 我正在参考我将要构建的其他包。

From the Bash shell, I have the following environment variables:在 Bash shell 中,我有以下环境变量:

    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:当我在 VS Code 的 DEBUG 中运行它时,这是错误:

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.我了解到的是,当我在终端中运行 Python 文件时,打印的操作系统环境变量是准确的,但是当我在调试中运行文件时,打印第一个操作系统环境变量时出错,说它不存在。

This is my ignorance on setting debug correctly.这是我对正确设置调试的无知。 Any pointers will help (and thank you for your responses)!**任何指示都会有所帮助(并感谢您的回复)!**

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.将环境变量添加到 launch.json 文件解决了这个问题。

From my experience, BASH does not like spaces, when declaring variables;根据我的经验,BASH 在声明变量时不喜欢空格;

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.可能会成功

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM