4
votes

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
Can you try uninstalling and reinstalling again ?(Some people reported this fixed their issue here although it was 2016, but it is worth a shot IMO) > I had to uninstall both google-cloud and also protobuf, then let google-cloud reinstall protobuf. - Ghasem Naddaf
I'll give this a shot and convey the outcome. Thanks for your response. - gremlin

3 Answers

2
votes

So I got it working upon importing storage as follows:

import google.datalab.storage as storage

1
votes

Have you installed the google-cloud-storage package in your DataLab environment, or on your local machine? You'll need to run the following command within DataLab:

!pip install google-cloud-storage

See https://cloud.google.com/datalab/docs/how-to/adding-libraries for more details

Also, the google-cloud package is deprecated, you shouldn't need to install it, see https://pypi.org/project/google-cloud/.

1
votes

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