Unable to import storage from google.cloud

Viewed 8974

I'm new to Google Cloud Platform, and have uploaded some machine learning code on Jupyter notebook in DataLab.

My issue is although, I installed Google Cloud Storage (using the command: pip install --upgrade google-cloud-storage), I'm unable to import this.

The following is how I'm importing this package:

>>import numpy    
>>import pandas as pd   
>>from google.cloud import storage

But I'm getting the following error:

ImportErrorTraceback (most recent call last) in () ----> 1 from google.cloud import storage

ImportError: cannot import name storage

Note:

  1. This is the content of my JSON config file: {"TokenSources":["env"]}
  2. I tried export GOOGLE_APPLICATION_CREDENTIALS="/path/to/file.json", but the error persists.
  3. I verified that this package is indeed installed in my environment by typing pip freeze in the command shell:

google-cloud==0.34.0

google-cloud-datastore==1.7.0

google-cloud-spanner==1.4.0

google-cloud-storage==1.10.0


What am I missing here?

3 Answers

So I got it working upon importing storage as follows:

import google.datalab.storage as storage

To make your notebooks resilient to both datalab and non-datalab environments you can use one of the the following methods for handling your import statements:

try:
  from google.cloud import storage
except ImportError:
  from google.datalab import storage

or

if 'google.datalab' in sys.modules:
  from google.datalab import storage
else:
  from google.cloud import storage

Alternatively if you would like to switch datalab to using from google.cloud import storage

Run the following in a cell

!pip install google-cloud-storage

Followed by this cell to reset the IPython kernel

# Reset the IPython kernel
from IPython.core.display import HTML
HTML("<script>Jupyter.notebook.kernel.restart()</script>")

Note: You need to reset the Python kernel after installation otherwise you will a ContextualVersionConflict error from naming conflicts

Related