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:
Using a combination of the techniques suggested by the above articles, I did the following:
Create a requirements.txt
file:
google-cloud==0.19.0
Import this library using pip
:
pip install -t lib -r requirements.txt
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.
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.
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.
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
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?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With