GAE Django + deferred requires webapp2 (No module named webapp2) - python

I'm running Django locally through Google App Engine's dev_appserver.py ./ and when I add this one line to my script:
from google.appengine.ext import deferred
I get the error: ImportError: No module named webapp2 from the deferred module.
I tried adding this to my libraries in app.yaml:
- name: webapp2
version: latest
But that doesn't work either. I feel like it doesn't make sense to use webapp2 when I'm running Django... Is it not possible to use deferred with Django on Google App Engine?
I'm trying to queue Django's send_mail() for a contact form, and I don't want the User to have to wait for it to be sent.
Not sure if this helps, but here's part of my Python Path which is displayed with the import error:
'/usr/local/google-cloud-sdk/platform/lib/MySQLdb-1.2.5',
'/usr/local/google-cloud-sdk/platform/lib/django-1.4',
'/usr/local/google-cloud-sdk/platform/lib/webapp2-2.5.2',
'/usr/local/google-cloud-sdk/platform/lib/protorpc-1.0',
'/usr/local/google-cloud-sdk/platform/lib/webob-1.1.1',
'/usr/local/google-cloud-sdk/platform/lib/yaml-3.10'
As you can see, it is loading webapp2 into the python path.

Related

Can't import Datastore from google.cloud

I'm trying to create a basic web app using Flask and Google Datastore to make myself to Google Cloud. Though, when I deploy my app, I get an Error 500, the details being that Python can't import Datastore : ImportError: No module named cloud.
Here is my app.yaml:
runtime: python27
api_version: 1
threadsafe: true
handlers:
- url: .*
script: main.app
libraries:
- name: jinja2
version: "2.6"
- name: markupsafe
version: "0.15"
- name: flask
version: 0.12
My main.py starts like follows:
from __future__ import absolute_import
# Standard imports
import time
import logging
import json
import threading
# Flask framework
from flask import request
from flask import Flask
# Google Cloud features
from google.cloud import datastore
# the following replaces requests
from google.appengine.api import urlfetch
Finally, my requirements.txt is the following:
Flask
google-cloud
click
When I deploy my app (using gcloud app deploy) and get to my site, I get my error 500.
I don't understand why I can't use from google.cloud import datastore since it's what Google does in their tutorial... I must be missing something but I can't find what.
Any help would be appreciated.
From Installing the client library:
pip install --upgrade google-cloud-datastore
The client library you need (to match your import statement) is google-cloud-datastore, but you don't have that listed in your requirements.txt file.
Side note: your app.yaml file indicates your app is a standard env GAE app one, for which you have the optimized Google Datastore NDB Client Library library available, already included in your SDK (if you don't have specific reasons for choosing the generic one instead).
You do not specify env: flex so you are using Standard Environment. Here it is described for flex yaml and here it doesn’t appear for standard yaml.
This is important because google-cloud is not supported for Standard Environment. google-cloud’s repository:
These libraries currently do not run on Google App Engine Standard
Link mentioned by Dan Cornilescu about the NDB Client is an oficial solution for standard environment and few examples can be found in the oficial documentation:
https://github.com/GoogleCloudPlatform/python-docs-samples/tree/master/appengine/standard/ndb and https://github.com/GoogleCloudPlatform/python-docs-samples/tree/master/appengine/standard/multitenancy

Cloud Endpoints - ImportError: No module named endpoints

I'm running into this weird error with a Cloud Endpoints app that I'm just starting to write. I'm not sure if Google changed their libraries or not, but I think this should work?
In my app.yaml I've got...
libraries:
- name: webapp2
version: "latest"
- name: endpoints
version: "latest"
And then in my main.py I call:
import endpoints
Result:
ImportError: No module named endpoints
Why would app engine be telling me that endpoints doesn't exist? I can see the endpoints folder in the directory itself...
I got the same thing on a fresh install of the SDK (previously worked).
You need to explicitly add the relevant GAE libraries to your PYTHONPATH.
For example if you're using virtualenvwrapper (you should be):
$ add2virtualenv /path/to/google-cloud-sdk/platform/google_appengine/lib/endpoints-1.0
$ add2virtualenv /path/to/google-cloud-sdk/platform/google_appengine/lib/protorpc-1.0

DJANGO_SETTINGS_MODULE undefined

I know that there a lot of questions regarding this and I have actually gone through them all but I am unable to solve the problem. I am trying to run a python webapp on (python 2.7) GAE by following a tutorial for GAE based on python 2.5 (from: Head First Python chap. 10). This requires the creation of a form using djangoforms. However, on runnning GAE server, I got the error: "ImportError: no module named django.core.exceptions".
After following the various discussions regarding this error on this forum, I came to know that I had to add the following two lines to my app.yaml file for python 2.7:
libraries
- name: django
version: "1.4"
and had to include the 'threadsafe' attribute too. I even had to 'rename' my app name with an extension of '.app' instead of '.py' so that I am able to use the 'threadsafe' attribute.
However, I get the error 'DJANGO_SETTINGS_MODULE is undefined'. I tried to install django 'exclusively'. I even (desperately) created a new django project and moved the settings file into a subfolder titled 'django' in my webapp. However, I am still getting errors. Currently, my app.yaml file looks like this:
application: hfwwg
version: 1
runtime: python27
api_version: 1
threadsafe: true
env_variables:
DJANGO_SETTINGS_MODULE: 'django/settings.py'
handlers:
- url: /.*
script: hfwwg.app
- url: /static
static_dir: static
libraries:
- name: django
version: "1.4"
Please suggest how to remove the 'DJANGO_SETTINGS_MODULE' error or any other place where there is error. Any help would be appreciated.
And here is the portion of the code where I have used django:
from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app
from google.appengine.ext import db
from google.appengine.ext.webapp import template
from google.appengine.ext.db import djangoforms
...
class MyForm(djangoforms.ModelForm):
...
DJANGO_SETTINGS_MODULE: 'django/settings.py'
this should be
DJANGO_SETTINGS_MODULE: 'django.settings'
LINK

Google App Engine module's routing is not working

I have a demo application written in Python and running on Google App Engine. This app has 3 modules. I added a configuration file to configure module's routing but it's not working. This file called dispatch.yaml looks like this:
application: dotted-lexicon-638
dispatch:
- url: "dotted-lexicon-638.appspot.com/"
module: default
- url: "dotted-lexicon-638.appspot.com/list*"
module: list
- url: "dotted-lexicon-638.appspot.com/logs*"
module: logs
I deployed this file using "appcfg.py update_dispatch project_dir" command
if you go to "http://list.dotted-lexicon-638.appspot.com" and "http://logs.dotted-lexicon-638.appspot.com"
you can see that both modules are working.
Am I missing something?
When you use a path for module routing, it doesn't mean that path maps to the root of the module it just determines which module gets a chance to handle the request. Unless you have code in your list module handling /list, then it will 404.

GAE - Flask can't import Jinja2 from subdirectory?

I'm working on a Google App Engine project using Flask. Flask is then using Jinja2. When I put Flask and all its required modules into the root folder of my project, the server started up just fine. However, I wanted to clean up the directory a bit so I moved Flask and other modules (including Jinja2) to a subdirectory called 'lib'. So my project looks like:
app.yaml
main.py
myapp
__init__.py
view.py
blahblah.py
lib
flask
jinja2
OtherModules
Then in the main.py file of the app I add the directory using sys.path.insert(0, 'lib'). Flask seems to import fine using this method, but Flask does not seem to be able to find Jinja2 with them both in the lib folder. When attempting to access a view on my running dev_appserver test I get:
File "lib/flask/__init__.py", line 19, in <module>
from jinja2 import Markup, escape
ImportError: No module named jinja2
How can I allow Flask to find Jinja2 (and allow other modules to find their requirements) while keeping them in the lib directory and not having to edit the modules to adjust paths?
Below is my main.py file in case something in there would be useful to know:
import sys
sys.path.insert(0, 'lib')
from google.appengine.ext.webapp.util import run_wsgi_app
from myapp import app
run_wsgi_app(app)
In order to include Jinja in you app engine application in your app.yaml file add these lines
libraries:
- name: jinja2
version: latest
Documentation for including more of the available libraries.
In order to use it for your local server you should install these also to your system. In a unix like system it would be
sudo easy_install jinja2
Additional information: there is an open source framework called gae-init, which combines your tech stack and provides a series of automations and good practices for app engine web services. Maybe worth having a look at it.

Categories