Python webapp Google app engine: no module named six - google-app-engine

The following code should take 2 dates from input.html and display months and years in output.html, but the Google app engine returns an error saying module six is missing, even though I have added all the site packages in my project library.
What am I doing wrong?
import webapp2
import jinja2
import os
import time
import datetime
import sys
from dateutil.rrule import rrule, MONTHLY
sys.path.append(os.path.join(os.path.dirname(__file__), "libs"))
template_dir = os.path.join(os.path.dirname(__file__), 'templates')
jinja_env = jinja2.Environment(loader = jinja2.FileSystemLoader(template_dir),
autoescape = True)
def render_str(template, **params):
t = jinja_env.get_template(template)
return t.render(params)
class MainHandler(webapp2.RequestHandler):
def write(self, *a, **kw):
self.response.out.write(*a, **kw)
def render_str(self, template, **params):
return render_str(template, **params)
def render(self, template, **kw):
self.write(self.render_str(template, **kw))
def get(self):
self.render('input.html')
def post(self):
frmstring=self.request.get('from')
tostring=self.request.get('to')
frm=time.strptime(frms,"%Y-%m")
to=time.strptime(tos,"%Y-%m")
dates = [dt for dt in rrule(MONTHLY, dtstart=frm, until=to)]
months_choices = []
for i in range(1,13):
months_choices.append(datetime.date(2008, i, 1).strftime('%B'))
self.render('output.html',dates = dates,months_choices=months_choices)
app = webapp2.WSGIApplication([
('/', MainHandler)
], debug=True)

You need to vendor six into your environment.
Run
pip install -t lib six
Then add these lines to your apppengine_config.py file (or create it)
# appengine_config.py
from google.appengine.ext import vendor
# Add any libraries install in the "lib" folder.
vendor.add('lib')
See https://cloud.google.com/appengine/docs/standard/python/tools/using-libraries-python-27#installing_a_third-party_library for more details

The six module is not provided by the AppEngine environment. Have you included it in your lib/ directory?

Related

Google App Engine trouble using Mail API

Hello I have a contact form with a post method on my contacts page and want to be able to use the Mail API to send outgoing mail from my google app engine application So I decided to use the mail API and registered myself as an authorized sender on my developer console and initiated the command --enable_sendmail=yes applicationID however when I test it, it gives me this error. I'm still fairly new to google app engine and not sure why that is happening.
"INFO 2015-08-06 18:44:50,935 mail_stub.py:142] MailService.Send
From: abdallahozaifa19527#gmail.com
To: Hozaifa
Subject: Test
/bin/sh: 1: sendmail: not found
ERROR 2015-08-06 18:44:50,944 mail_stub.py:263] Error sending mail using sendmail: [Errno 32] Broken pipe"
import os
import webapp2
import jinja2
from google.appengine.api import mail
template_dir = os.path.join(os.path.dirname(__file__), 'templates')
jinja_env = jinja2.Environment(loader = jinja2.FileSystemLoader(template_dir),
autoescape = True)
class Handler(webapp2.RequestHandler):
def write(self, *a, **kw):
self.response.out.write(*a, **kw)
def render_str(self, template, **params):
t = jinja_env.get_template(template)
return t.render(params)
def render(self, template, **kw):
self.write(self.render_str(template, **kw))
class MainPage(Handler):
def get(self):
self.render("index.html")
class AboutPage(Handler):
def get(self):
self.render("about.html")
class ProjectsPage(Handler):
def get(self):
self.render("projects.html")
class ExperiencePage(Handler):
def get(self):
self.render("experience.html")
class ContactPage(Handler):
def get(self):
self.render("contact.html")
def post(self):
name = self.request.get('name')
email = self.request.get('email')
message = self.request.get('message')
if (name and email and message):
message = mail.EmailMessage(sender="abdallahozaifa19527#gmail.com", subject="Test")
message.to = "Hozaifa<abdallahozaifa19527#gmail.com>"
message.send()
else:
self.write("It didnt work!")
app = webapp2.WSGIApplication([('/', MainPage),
('/about.html', AboutPage),
('/projects.html', ProjectsPage),
('/experience.html', ExperiencePage),
('/contact.html', ContactPage)])
My code works, however it does not work with the google app engine SDK because the SDK doesn't have the send mail, I was trying to test it using the SDK, problem solved!

Unittest Jinja2 and Webapp2 : template not found

I use Jinja2 with Webapp2 on a GAE project.
I have a base RequestHandler as describe in webapp2_extras.jinja2:
import webapp2
from webapp2_extras import jinja2
def jinja2_factory(app):
"""Set configuration environment for Jinja."""
config = {my config...}
j = jinja2.Jinja2(app, config=config)
return j
class BaseHandler(webapp2.RequestHandler):
#webapp2.cached_property
def jinja2(self):
# Returns a Jinja2 renderer cached in the app registry.
return jinja2.get_jinja2(factory=jinja2_factory, app=self.app)
def render_response(self, _template, **context):
# Renders a template and writes the result to the response.
rv = self.jinja2.render_template(_template, **context)
self.response.write(rv)
And a view handler as:
class MyHandler(BaseHandler):
def get(self):
context = {'message': 'Hello, world!'}
self.render_response('my_template.html', **context)
My templates are in the default location (templates).
The app works well on dev server, and the template is correctly rendered.
But when I try to unittest MyHandler with
import unittest
import webapp2
import webstest
class MyHandlerTest(unittest.TestCase):
def setUp(self):
application = webapp2.WSGIApplication([('/', MyHandler)])
self.testapp = webtest.TestApp(application)
def test_response(self):
response = application.get_response('/')
...
application.get_response('/my-view') raise an exception: TemplateNotFound: my_template.html.
Is there something I missed? Like a jinja2 environment or template loader configuration?
Problem origin:
Jinja2 default loader searches files in a relative ./templates/ directory. When you run your GAE application on the development server this path is relative to the root of your application. But when you run your unittests this path is relative to your unittest files.
Solution:
Not really an ideal solution, but here a trick I did to solve my problem.
I updated the jinja2 factory to add a dynamic template path, set in app config:
def jinja2_factory(app):
"""Set configuration environment for Jinja."""
config = {'template_path': app.config.get('templates_path', 'templates'),}
j = jinja2.Jinja2(app, config=config)
return j
And I set an absolute path to the templates in the setUp of my unittests:
class MyHandlerTest(unittest.TestCase):
def setUp(self):
# Set template path for loader
start = os.path.dirname(__file__)
rel_path = os.path.join(start, '../../templates') # Path to my template
abs_path = os.path.realpath(rel_path)
application.config.update({'templates_path': abs_path})

How do I fix this TypeError?

I'm new to python and Google App Engine. I'm trying to refactor this code from Nick Johnson blog to use webapp2 and python 2.7. http://blog.notdot.net/2009/10/Blogging-on-App-Engine-part-1-Static-serving
Anyways, when I run the code below I get this error.
TypeError: get() takes exactly 2 arguments (1 given)
I think it may have something to do with the path variable not being defined, but I don't know how to define it.
import webapp2
from google.appengine.ext import webapp
from google.appengine.ext import db
class StaticContent(db.Model):
body = db.BlobProperty()
content_type = db.StringProperty(required=True)
last_modified = db.DateTimeProperty(required=True, auto_now=True)
def get(path):
return StaticContent.get_by_key_name(path)
def set(path, body, content_type, **kwargs):
content = StaticContent(
key_name=path,
body=body,
content_type=content_type,
**kwargs)
content.put()
return content
class MainHandler(webapp2.RequestHandler):
def get(self, path):
content = get(path)
if not content:
self.error(404)
return
app = webapp2.WSGIApplication([('/', MainHandler)],
debug=True)
The error is raised because the get method of the MainHandler class expects a path parameter.
You should add grouping to the regex in your routing definition to pass the path parameter to the get method:
app = webapp2.WSGIApplication([('(/.*)', MainHandler)],
debug=True)

Appengine serve gzipped files

I'm using AppEngine to store some pickled python objects in my app. I want to serve these to the user directly, and I'm simply using the X-AppEngine-Blobkey header to serve the files to the user with a file.pickle.gz filename. However, when I try to extract these on my computer (Mac OS) using a simple double click, the files are turned into file.pickle.gz.cpgz.
I thought it was my browser being sneaky and extracting them, but I don't think so, since
pickle.load('file.pickle.gz')
Doesn't work, and neither does
pickle.load('file.pickle.gz.cpgz')
To store the files, I use:
blobfile = files.blobstore.create(mime_type='application/gzip')
with files.open(blobfile, 'a') as f:
gz = gzip.GzipFile(fileobj=f,mode='wb')
gz.write(my_pickled_object)
gz.close()
files.finalize(blobfile)
I think I'm not understanding the way gzips work. Can someone explain?
Are you sure file.pickle.gz.cpgz is the result of your double-clicking on the file.pickle.gz file you downloaded? Usually ".cpgz" is a different kind of archive file.
I can get the code you posted to work in a development server without significant changes. Here's the code, if it helps:
#!/usr/bin/env python
from __future__ import with_statement
import gzip
import pickle
from google.appengine.api import files
from google.appengine.api import memcache
from google.appengine.ext import blobstore
from google.appengine.ext import webapp
from google.appengine.ext.webapp import blobstore_handlers
from google.appengine.ext.webapp import util
class MainHandler(webapp.RequestHandler):
def get(self):
self.response.out.write('Hello world! make get')
class MakeFileHandler(webapp.RequestHandler):
def get(self):
data = pickle.dumps({'a':1, 'b':True, 'c':None})
blobfile = files.blobstore.create(mime_type='application/gzip')
with files.open(blobfile, 'a') as f:
gz = gzip.GzipFile(fileobj=f,mode='wb')
gz.write(data)
gz.close()
files.finalize(blobfile)
memcache.set('filekey', files.blobstore.get_blob_key(blobfile))
self.redirect('/')
class GetFileHandler(blobstore_handlers.BlobstoreDownloadHandler):
def get(self):
blobkey = memcache.get('filekey')
if blobkey:
self.send_blob(blobkey)
else:
self.response.out.write('No data key set back')
def main():
application = webapp.WSGIApplication([('/', MainHandler),
('/make', MakeFileHandler),
('/get', GetFileHandler)],
debug=True)
util.run_wsgi_app(application)
if __name__ == '__main__':
main()
Click on "make", then click on "get". A file named "get.gz" is downloaded to your ~/Downloads/ folder (at least in Chrome). Double-click on it to produce a file named "get". Then:
% python
>>> import pickle
>>> pickle.load(open('get'))
{'a': 1, 'c': None, 'b': True}

Web2py Google App Engine Testing

I'm building an GAE app with web2py and am struggling with setting up a test framework.
I've looked into:
web2py_utils
nosegae
Googler Ikai Lan's suggestions
gaetestbed which was merged into GAE apis
Here's a unit test I attempted:
import unittest
import nose
from nose.tools import *
from google.appengine.ext import testbed
from google.appengine.datastore import datastore_stub_itil
class UserModelTest(unittest.TestCase):
def setUp(self):
# First, create an instance of the Testbed class.
self.testbed = testbed.Testbed()
# Then activate the testbed, which prepares the service stubs for use.
self.testbed.activate()
# Initialize the datastore stub with this policy.
self.testbed.init_datastore_v3_stub(consistency_policy=self.policy)
self.env = new_env(app='fb', controller='default')
self.db = copy_db(self.env, db_name='db', db_link='sqlite:memory')
self.user = self.db.auth_user.insert(first_name='Bob', last_name='Narley', fb_id=1, password='testtest')
self.dream = self.db.dream.insert(title='Graduate UC Santa Cruz with Computer Science degree by June 8, 2012.',
type='education', owner = self.user, progress=92.0)
self.task1 = self.db.task.insert(dream=self.dream, title='Buy batteries for calculator', solution='Go to Walmart at 12:00pm October 30, 2012 and buy Duracell AAA.',
status=1)
self.task2 = self.db.task.insert(dream=self.dream, title='Make Winston happy',
solution='Make banana milk',
status=0)
self.user.update_record(tasks=[self.task1, self.task2])
def tearDown(self):
self.testbed.deactivate()
def test_create_user(self):
assert_equal('Bob', self.user.first_name)
assert_equal(1, self.user.fb_id)
assert_equal('testtest', self.user.password)
def test_user_has_many_tasks(self):
tasks = self.db(self.db.task.id.belongs(self.user.tasks)).select()
assert_equal(2, len(tasks))
run_fb_tests.py:
from web2py_utils.test_runner import run
import sys
run(path = sys.path[0],
app = 'fb',
test_key = 'superSecret',
test_options = {'verbosity': 3, 'with-gae': True, 'without-sandbox': True},
coverage_report = 'logs/coverage_report.txt',
DO_COVER = True,
DO_NOSE = True,)
I get the following error when execute my run_fb_tests.py
ImportError: No module named google.appengine.ext
I've been banging my head for days now with many errors. This is only one of many
How do you setup a testing framework in web2py for GAE apps?
Where is your script run_fb_tests.py? If it is in the web2py folder I get a similar error, but if you place it in google_appenine, or in the containing folder of the google_appengine then I get no import errors.

Resources