Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to use google-cloud in a GAE app

The following line in my Google App Engine app (webapp.py) fails to import the Google Cloud library:

from google.cloud import storage

With the following error:

ImportError: No module named google.cloud.storage

I did some research and found the following articles to be helpful:

  • https://cloud.google.com/appengine/docs/python/tools/using-libraries-python-27#installing_a_library
  • https://stackoverflow.com/a/34585485
  • https://www.simonmweber.com/2013/06/18/python-protobuf-on-app-engine.html

Using a combination of the techniques suggested by the above articles, I did the following:

  1. Create a requirements.txt file:

    google-cloud==0.19.0
    
  2. Import this library using pip:

    pip install -t lib -r requirements.txt
    
  3. Use the following code in my appengine_config.py file:

    import os
    import sys
    import google
    libDir = os.path.join(os.path.dirname(__file__), "lib")
    google.__path__.append(os.path.join(libDir, "google"))
    sys.path.insert(0, libDir)
    

Can anyone shed light on what I might be missing to get this working? I'm just trying to write a Google App Engine app that can write/read from Google Cloud Storage, and I'd like to test locally before deploying.

like image 229
nefaurk Avatar asked Sep 26 '16 13:09

nefaurk


People also ask

Can't connect to Google Play Games?

Make sure you have updated your game to the latest version. Try clearing your cache by going to Settings > Application Manager > Google Play Services > Clear Data/Cache. Make sure that you are using the latest Google Play Services and Google Play Games versions by opening Google Play and visiting the My Apps menu.

Can I run app on Google Cloud?

Google has built Cloud Run to work well together with other services on Google Cloud, so you can build full-featured applications. In short, Cloud Run allows developers to spend their time writing their code, and very little time operating, configuring, and scaling their Cloud Run service.


2 Answers

It looks like the only thing that is required is to include google-cloud into your project requirements.txt file.

Check if this simple sample works for you (you shouldn't get any imports error). Create below files and run pip install -r requirements.txt -t lib. Nothing more is required on my site to make it work.

app.yaml

application: mysample
runtime: python27
api_version: 1
threadsafe: true

handlers:
  - url: /.*
    script: main.app

main.py

import webapp2
from google.cloud import storage


class MainPage(webapp2.RequestHandler):
    def get(self):
        self.response.headers['Content-Type'] = 'text/plain'
        self.response.write('Hello, World!')

app = webapp2.WSGIApplication([
    ('/', MainPage),
], debug=True)

appengine_config.py

from google.appengine.ext import vendor
import os

# Third-party libraries are stored in "lib", vendoring will make
# sure that they are importable by the application.
if os.path.isdir(os.path.join(os.getcwd(), 'lib')):
    vendor.add('lib')

requirements.txt

google-cloud
like image 97
manRo Avatar answered Oct 12 '22 23:10

manRo


There is an App Engine specific Google Cloud Storage API that ships with the App Engine SDK that you can use to work with Cloud Storage buckets.

import cloudstorage as gcs

Is there a reason you didn't use this built-in library, which requires no configuration to load?

like image 39
BrettJ Avatar answered Oct 13 '22 00:10

BrettJ