环境变量不适用于GCP Windows 10

2024-04-19 18:26:46 发布

您现在位置:Python中文网/ 问答频道 /正文

我目前正在使用Google云平台的语音到文本Api。我已经按照提示阅读了开发人员站点上的一些可能的错误和疑难解答信息。但我似乎无法创建访问auth凭据所需的env变量。你知道吗

变量的名称是GOOGLE\u APPLICATION\u CREDENTIALS。我尝试了以下方法:

  1. 使用命令提示符设置变量:set GOOGLE_APPLICATION_CREDENTIALS=[path]
  2. 使用anaconda power shell设置变量:$env:GOOGLE_APPLICATION_CREDENTIALS=[path]
  3. 在windows10上的environment variables native选项中包含path in path variables
  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))

我读过关于设置和验证环境变量的其他答案,但没有解决方案奏效。你知道吗

我还可以在anacondashell提示符的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.

Tags: thepathinfromimportclientcloudapplication