【问题标题】:Environment variable not working with GCP- Windows 10环境变量不适用于 GCP-Windows 10
【发布时间】:2019-10-30 16:15:57
【问题描述】:

我目前正在使用 Google Cloud Platform Speech-to-Text Api。我已经按照提示阅读了开发者网站上的一些可能的错误和故障排除信息。但我似乎无法制作访问身份验证凭据所需的 env 变量。

变量的名称是 GOOGLE_APPLICATION_CREDENTIALS。我尝试了以下方法:

  1. 使用命令提示符设置变量:set GOOGLE_APPLICATION_CREDENTIALS=[path]
  2. 使用 anaconda power shell 设置变量:$env:GOOGLE_APPLICATION_CREDENTIALS=[path]
  3. 在 Windows 10 上的环境变量本机选项的 PATH 变量中包含路径
  4. 在上述相同的本机选项状态中包含环境变量。
  5. 使用以下方法在 Sublime3 中设置变量:
def explicit():
    from google.cloud import storage

    # Explicitly use service account credentials by specifying the private key
    # file.
    storage_client = storage.Client.from_service_account_json(
        'path')

    # Make an authenticated API request
    buckets = list(storage_client.list_buckets())
    print(buckets)

我尝试运行的代码如下:

import io
import os

# Imports the Google Cloud client library
from google.cloud import speech
from google.cloud.speech import enums
from google.cloud.speech import types


# Instantiates a client
client = speech.SpeechClient()

# The name of the audio file to transcribe
file_name = os.path.join(
    os.path.dirname(__file__),
    'resources',
    'audio.raw')

# Loads the audio into memory
with io.open(file_name, 'rb') as audio_file:
    content = audio_file.read()
    audio = types.RecognitionAudio(content=content)

config = types.RecognitionConfig(
    encoding=enums.RecognitionConfig.AudioEncoding.LINEAR16,
    sample_rate_hertz=16000,
    language_code='en-US')

# Detects speech in the audio file
response = client.recognize(config, audio)

for result in response.results:
    print('Transcript: {}'.format(result.alternatives[0].transcript))

我已阅读有关设置和验证环境变量的其他答案,但没有任何解决方案有效。

我还可以在 anaconda shell 提示符中看到 env 文件夹中的环境。有没有人知道为什么脚本找不到文件?

错误信息是:

line 317, in default
    raise exceptions.DefaultCredentialsError(_HELP_MESSAGE)
google.auth.exceptions.DefaultCredentialsError: Could not automatically determine credentials. Please set GOOGLE_APPLICATION_CREDENTIALS or explicitly create credentials and re-run the application.

【问题讨论】:

  • 我不确定这是否直接是一个 Sublime 问题;如果您从命令提示符手动运行程序与尝试在 Sublime 中运行程序,您会得到不同的结果吗?
  • 嗨@OdatNurd,你是对的,我确实遇到了同样的错误 - 我现在将编辑问题。
  • 您是否尝试过在您的 python 脚本中显式打印出环境变量?这至少可以验证脚本是否可以“看到”环境变量 - print(os.environ['GOOGLE_APPLICATION_CREDENTIALS'])
  • @elembie 是的,它给了我一个 KeyError

标签: python environment-variables speech-to-text google-developer-tools


【解决方案1】:

您好,我也遇到了同样的问题,但后来解决了。所以路径没有被设置的原因可能是因为文件被系统阻止了。只需转到文件所在的位置,然后右键单击文件-> 属性。在那里你会看到一个名为取消阻止的选项。单击它,应该可以解决您的问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-05-03
    • 1970-01-01
    • 2014-12-18
    • 2016-04-25
    • 1970-01-01
    • 2023-02-02
    • 2019-08-11
    • 2018-10-15
    相关资源
    最近更新 更多