Cannot use Google Cloud in GAE app

The following line in the Google App Engine ( webapp.py) application does not allow importing 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 helpful:

Using a combination of the methods suggested in the above articles, I did the following:

  • Create a file requirements.txt:

    google-cloud==0.19.0
    
  • Import this library with pip:

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

    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)
    

- , , ? Google App Engine, / Google, .

+4
6

API- Google Cloud Storage API , SDK Engine, .

import cloudstorage as gcs

, , ?

+2

, , , - google-cloud requirements.txt.

, ( ). pip install -r requirements.txt -t lib. , .

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
+4

appengine_config.py :

from google.appengine.ext import vendor
vendor.add('lib')

, , .

+1

, , github issue . (pip install gcloud), :

from gcloud import storage

, appengine_config.py, dyeray.

, , , 0.20.0 google-cloud. . pip install --upgrade google-cloud

+1

"import google" sys.path.insert

0

, .

1: app.yaml , :

runtime: python27
api_version: 1
threadsafe: no

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

libraries:
- name: jinja2
  version: "2.6"
- name: markupsafe
  version: "0.15

main.py script :

from flask import Flask
app = Flask(__name__)
app.config['DEBUG'] = True

@app.route('/')
def hello():
    """Return a friendly HTTP greeting."""
    return 'Hello World!'

Google .

2. ImportModule error -
app.yaml script=main.app script=main.app

from flask import Flask
app = Flask(__name__)
app.config['DEBUG'] = True

main.py file. appengine_config.py sys. , , Setup 1, main.py .

from google.appengine.ext import vendor
vendor.add('lib')

. , .

0

Source: https://habr.com/ru/post/1655848/


All Articles