python - 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'
解决方案
第一代 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']
推荐阅读
- r - geom_line 如何同时控制线条颜色和图例
- python - 如何使用按钮在 Django 中执行视图?
- python - 如何从熊猫的数据框列表中选择前 n 列?
- c - 如何使用正则表达式使用 sscanf 进行提取?(在 C 中)
- c# - 将 Powershell Object[] 传递给 C# 方法的最佳方法
- selenium - 在继续测试的同时等待 Web 元素
- excel - 在 sumif 和 sumifs 之间切换
- google-analytics - 缩小 _gaq.push 和 gtag 之间的差距
- c++ - 函数调用中的 C++ Int 数组类型
- winforms - 如何使用 SAML 对 Windows 窗体客户端进行身份验证?