【问题标题】:import cloudstorage, SyntaxError: invalid syntax导入云存储,SyntaxError:无效语法
【发布时间】:2018-02-01 10:12:38
【问题描述】:

我想使用Google Cloud Storage Client Library Functions

为此,我必须import cloudstorage。要获得cloudstorage,我下载Google Cloud Storage client library

我尝试使用python -c "import cloudstorage" 导入cloudstorage。 我收到以下错误:

Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/Users/fghavamian/Documents/PhD_work/Codes/python/convnet_gcloud/cloudstorage/__init__.py", line 20, in <module>
    from .api_utils import RetryParams
  File "/Users/fghavamian/Documents/PhD_work/Codes/python/convnet_gcloud/cloudstorage/api_utils.py", line 173
    except self.retriable_exceptions, e:
                                    ^
SyntaxError: invalid syntax

我错过了什么吗?

【问题讨论】:

  • 我的猜测:您正在运行 Python 3,但看起来像 Python 2 语法(不再使用except Exception, e
  • @Chris_Rands;是的,这是因为使用了 Python3。当我使用 python 2 时,错误消失了。还有一个问题,现在我收到以下错误ImportError: No module named appengine.api
  • 您找到解决方案了吗?
  • 我在github 上打开了一个问题,但看起来google-cloud-python 是云存储的最新API。

标签: python google-cloud-storage cloud-storage


【解决方案1】:

正如其中一位 cmets 所说,这是使用 Python 3 的问题,其中语法已更改。您使用的 Google Cloud Storage 库似乎不太受支持,more recommended 库为 google-cloud-python

假设你已经安装了Google Cloud SDK,你可以使用下面的命令安装它:

pip install google-cloud-storage

这里是一些示例代码 (from the docs),展示了如何读取和写入存储桶。

from google.cloud import storage
client = storage.Client()
# https://console.cloud.google.com/storage/browser/[bucket-id]/
bucket = client.get_bucket('bucket-id-here')
# Then do other things...
blob = bucket.get_blob('remote/path/to/file.txt')
print(blob.download_as_string())  # read content from bucket and print to stdout
blob.upload_from_string('New contents!')
blob2 = bucket.blob('remote/path/storage.txt')
blob2.upload_from_filename(filename='/local/path.txt')  # write from path.txt to storage.txt

【讨论】:

    猜你喜欢
    • 2022-08-04
    • 1970-01-01
    • 2015-09-08
    • 1970-01-01
    • 2013-12-19
    • 2018-12-14
    • 2020-07-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多