49

I tried to run the code bellow by following the google tutorials i found here: https://cloud.google.com/docs/authentication/production

def implicit():
    from google.cloud import storage

    # If you don't specify credentials when constructing the client, the
    # client library will look for credentials in the environment.
    project = 'my_project_name'
    storage_client = storage.Client(project=project)

    # Make an authenticated API request
    buckets = list(storage_client.list_buckets())
    print(buckets)

implicit()

But it keeps giving me the following error:

Traceback (most recent call last):
  File "[PATH]/scratch_5.py", line 13, in <module>
    implicit()
  File "[PATH]/scratch_5.py", line 2, in implicit
    from google.cloud import storage
ImportError: cannot import name storage

Could someone help me with this?

3 Answers 3

100

I see you are trying to use the Google Cloud Storage client libraries.

In order to use it, you should first make sure that it is installed in your machine:

pip install --upgrade google-cloud-storage

And then, you should probably set up authentication (if you are using Application Default Credentials, from the documentation you mentioned), by setting up the GOOGLE_APPLICATION_CREDENTIALS environment variable in the machine where you are running the code, like below. If you are using Windows, follow the steps presented in the documentation, instead.

export GOOGLE_APPLICATION_CREDENTIALS="/path/to/file.json"

Alternatively, you can try using explicit credentials. The only difference between the one you shared (using implicit credentials obtained from the environment) and one using explicit credentials, is that when you declare the GCS client, you should do something like:

storage_client = storage.Client.from_service_account_json('/path/to/SA_key.json')

Once all this is ready, you should have no issues with running the sample code you provided. In order to keep learning about GCS and its client libraries, feel free to search on the documentation I linked and have a look at the library reference page.

7
  • I had already done both of those steps. But just so I could, like, be 100% sure is not that. I did it again and it gives me a totally different error, it says that I Could not automatically determine credentials Commented Jun 13, 2018 at 16:19
  • Then it looks like the library was not installed (or at least not properly), as now it is not failing with cannot import name storage and is instead failing in a later step, when trying to obtain the credentials. Now, it can be one of two things, either you did not pass the correct credentials to the environment variable (note that the JSON file I mentioned must be a valid key for one of the Service Accounts in your project); or you can try using explicit credentials.
    – dsesto
    Commented Jun 14, 2018 at 7:02
  • Also, where are you running this code from? Is this your personal compute, a VM instance in GCE?
    – dsesto
    Commented Jun 14, 2018 at 7:16
  • I tried using the explicit way and it worked just fine with the same json key file. Also, I am running it in my personal computer with pycharm. Thank you very much for your help! I am now just curious about why wouldn't it do it implicitly. Commented Jun 14, 2018 at 17:45
  • 1
    best answer by far Commented Apr 3, 2022 at 3:25
4

I got the error cause i forgot to specify it in "requirements.txt"

3

Also, make sure your main.py file and the requirements.txt are in the same directory and the same directory as the function being deployed.

Just FYI, because I had to do this even after specifying my environment variables.

1
  • +1 I was having problems with a Pub/Sub app and adding google.cloud.storage to requirements.txt solved my problem. Commented Apr 15, 2021 at 15:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.