Unable to use google-cloud in a GAE app

2024/9/27 9:22:08

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.

Answer

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: truehandlers:- url: /.*script: main.app

main.py

import webapp2
from google.cloud import storageclass 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
https://en.xdnf.cn/q/71467.html

Related Q&A

Multiple thermocouples on raspberry pi

I am pretty new to the GPIO part of the raspberry Pi. When I need pins I normally just use Arduino. However I would really like this project to be consolidated to one platform if possible, I would li…

Strange behaviour when mixing abstractmethod, classmethod and property decorators

Ive been trying to see whether one can create an abstract class property by mixing the three decorators (in Python 3.9.6, if that matters), and I noticed some strange behaviour. Consider the following …

Center the third subplot in the middle of second row python

I have a figure consisting of 3 subplots. I would like to locate the last subplot in the middle of the second row. Currently it is located in the left bottom of the figure. How do I do this? I cannot …

Removing columns which has only nan values from a NumPy array

I have a NumPy matrix like the one below:[[182 93 107 ..., nan nan -1][182 93 107 ..., nan nan -1][182 93 110 ..., nan nan -1]..., [188 95 112 ..., nan nan -1][188 97 115 ..., nan nan -1][188 95 112 ..…

how to get kubectl configuration from azure aks with python?

I create a k8s deployment script with python, and to get the configuration from kubectl, I use the python command:from kubernetes import client, configconfig.load_kube_config()to get the azure aks conf…

Object vs. Dictionary: how to organise a data tree?

I am programming some kind of simulation with its data organised in a tree. The main object is World which holds a bunch of methods and a list of City objects. Each City object in turn has a bunch of m…

Fastest way to compute distance beetween each points in python

In my project I need to compute euclidian distance beetween each points stored in an array. The entry array is a 2D numpy array with 3 columns which are the coordinates(x,y,z) and each rows define a ne…

Calling C from Python: passing list of numpy pointers

I have a variable number of numpy arrays, which Id like to pass to a C function. I managed to pass each individual array (using <ndarray>.ctypes.data_as(c_void_p)), but the number of array may va…

Use of initialize in python multiprocessing worker pool

I was looking into the multiprocessing.Pool for workers, trying to initialize workers with some state. The pool can take a callable, initialize, but it isnt passed a reference to the initialized worker…

Pandas: select the first couple of rows in each group

I cant solve this simple problem and Im asking for help here... I have DataFrame as follows and I want to select the first two rows in each group of adf = pd.DataFrame({a:pd.Series([NewYork,NewYork,New…