首页 > 解决方案 > Google Cloud Functions:app_identity 获取 APPLICATION_ID 所需的正确模块

问题描述

如何在 Python Google Cloud 函数中正确导入 app_identity 模块,以便检索APPLICATION_ID/ PROJECT_ID

由于许多解决方案是专门为 Google App Engine 部署量身定制的,这会很有帮助。我部署了一个 GCF 并使用以下代码导入了app_identity模块:

from google.appengine.api import app_identity

我收到了这个错误: ModuleNotFoundError: No module named 'google.appengine'

标签: pythongoogle-cloud-platformgoogle-cloud-functions

解决方案


第一代 App Engine 模块 ( google.appengine) 仅存在于原始 Python 2.7 运行时中。它们不适用于 Cloud Functions 或 App Engine 上的 Python 3.7 运行时。

相反,您可以从环境变量中获取这些内容:

import os
project_id = os.environ['GOOGLE_CLOUD_PROJECT']
function_name = os.environ['FUNCTION_NAME']

推荐阅读