简体   繁体   中英

Unable to use google.cloud (storage) from within a python script

I am trying to run the following simple script

#!/usr/local/bin/python3.9

from google.cloud import storage

client = storage.Client()
for blob in client.list_blobs('my-bucket', prefix='some/path'):
  print(str(blob))

This fails as follows:

▶ ./test.py  
Traceback (most recent call last):
  File "/Users/pkaramol/Desktop/./test.py", line 3, in <module>
    from google.cloud import storage
ModuleNotFoundError: No module named 'google.cloud'

However, google-cloud-storage is already installed:

▶ pip freeze | grep -i google
google-api-core==2.11.0
google-auth==2.15.0
google-cloud==0.34.0
google-cloud-core==2.3.2
google-cloud-storage==2.7.0
google-crc32c==1.5.0
google-resumable-media==2.4.0
googleapis-common-protos==1.57.0

Why is that?

You can directly install the following Python package in your virtual env:

requirements.txt file:

google-cloud-storage==2.7.0

Check of your virtual env was correctly created with the following command:

which python3

Install the package:

pip install -r requirements.txt

Normally with this package, the import and your program will be correct in your virtual env:

from google.cloud import storage

client = storage.Client()
for blob in client.list_blobs('my-bucket', prefix='some/path'):
  print(str(blob))

You need to be sure you are correctly created your virtual env and installed the expected package inside.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM