5

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?

Tot Zam
  • 8,406
  • 10
  • 51
  • 76
gremlin
  • 71
  • 1
  • 1
  • 3
  • Can you try uninstalling and reinstalling again ?(Some people reported this fixed their issue [here](https://github.com/GoogleCloudPlatform/google-cloud-python/issues/2464) 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 Aug 29 '18 at 19:38
  • I'll give this a shot and convey the outcome. Thanks for your response. – gremlin Aug 29 '18 at 23:42

3 Answers3

3

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/.

Dustin Ingram
  • 20,502
  • 7
  • 59
  • 82
  • I did run the pip install within DataLab. But thanks for your response. – gremlin Aug 29 '18 at 23:45
  • 1
    It's possible that the existence of the deprecated `google-cloud` package is somehow interfering, can you remove it, reinstall `google-cloud-storage` and confirm that it's still not working? – Dustin Ingram Aug 30 '18 at 15:15
2

So I got it working upon importing storage as follows:

import google.datalab.storage as storage

gremlin
  • 71
  • 1
  • 1
  • 3
1

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

ScottMcC
  • 4,094
  • 1
  • 27
  • 35