Skip to main content
 首页 » 编程设计

python之Python 2.7 中的 Google Cloud 客户端库

2024年11月24日9wayfarer

我一直在为我的项目使用 Google Cloud 客户端库,但出现导入错误。

Import Error: No module named cloud

我的项目中有 app.yaml,如下所示:

runtime: python27 
 
api_version: 1 
threadsafe: no 
 
handlers: 
- url: /.* 
  script: main 

现在我已经配置了 requirements.txt,其中包含 google-cloud-datastore 和 google-cloud-storage

requirements.txt

Flask==1.0.2 
google-cloud-datastore 
google-cloud-storage 
google-api-python-client 

现在,当我在 main.py 文件中导入 google.cloud 时,出现导入错误。

from google.cloud import storage 

我已经在 Google App Engine 上部署了项目

出现错误后,我将项目更改为 Python 3.7,然后部署到 App Engine 上,令我惊讶的是,它运行完美。

这是因为 google-api-python-client 在 Python 2.7 中被弃用了还是我在这里做错了什么

请您参考如下方法:

请注意,google-cloud-storage 不支持 Python 2.7 的 App Engine 标准环境。幸运的是,Google Cloud 为 Python 2.7 提供了替代方案。所以,要使用谷歌云存储,你需要安装GoogleAppEngineCloudStorageClient .

在lib的父目录下,使用这个命令:

pip install -t lib GoogleAppEngineCloudStorageClient 

然后,您可以像这样导入cloudstorage:

import cloudstorage 

请查找the official documentation : 这个例子是用 webapp2 精心制作的,但它适用于 Flask。