I must be doing something stupid. I'm running this in Google App Engine:
class MainHandler(webapp.RequestHandler):
def render(self, template_name, template_data):
path = os.path.join(os.path.dirname(__file__), 'static/templates/%s.html' % template_name)
self.response.out.write(template.render(path, template_data)) # error here
def get(self):
self.response.out.write("hi")
def main():
application = webapp.WSGIApplication([('/', MainHandler)],
debug=True)
util.run_wsgi_app(application)
if __name__ == '__main__':
main()
This gives an error:
Traceback (most recent call last):
File "C:\Program Files\Google\google_appengine\google\appengine\tools\dev_appserver.py", line 3192, in _HandleRequest
self._Dispatch(dispatcher, self.rfile, outfile, env_dict)
File "C:\Program Files\Google\google_appengine\google\appengine\tools\dev_appserver.py", line 3135, in _Dispatch
base_env_dict=env_dict)
File "C:\Program Files\Google\google_appengine\google\appengine\tools\dev_appserver.py", line 516, in Dispatch
base_env_dict=base_env_dict)
File "C:\Program Files\Google\google_appengine\google\appengine\tools\dev_appserver.py", line 2394, in Dispatch
self._module_dict)
File "C:\Program Files\Google\google_appengine\google\appengine\tools\dev_appserver.py", line 2304, in ExecuteCGI
reset_modules = exec_script(handler_path, cgi_path, hook)
File "C:\Program Files\Google\google_appengine\google\appengine\tools\dev_appserver.py", line 2200, in ExecuteOrImportScript
exec module_code in script_module.__dict__
File "main.py", line 22, in <module>
class MainHandler(webapp.RequestHandler):
File "main.py", line 38, in MainHandler
self.writeOut(template.render(path, template_data))
NameError: name 'self' is not defined
What am I doing wrong?
The exception is happening while the class is being defined, which means that your indentation is off. Tabs in Python are equivalent to 8 spaces, so if all the preceding lines are using tabs and your tabstop is set to 4 spaces then the indentation only looks correct.
Just in case someone happens upon this and is looking for a solution not having to do with indentation, this is a good reference for how / when to use self.
NameError: name 'self' is not defined
Most style guides for python, including the google style guide, recommend you use spaces instead of tabs... most text editors support this too. Helps you avoid mistakes like this.
Related
I’m following a video from 2012 - Gevent-socketio, cross-framework real-time web live demo (https://www.youtube.com/watch?v=zhh_N5pmHBY)
I’m working on Ubuntu 15.04.
At 8 mins, in init.py he corrects config.add_renderer('.html', 'pyramid.mako_templating.renderer_factory') but after that I still can’t get it to work. The error I’m getting is below.
Any and all help would be greatly appreciated.
Thank you
(env)cloud#cloudnetwork:~/Code/python/3/moo/Moo$ pserve --reload development.ini
Starting subprocess with file monitor
Traceback (most recent call last):
File "/home/cloud/Code/python/3/moo/env/bin/pserve", line 9, in <module>
load_entry_point('pyramid==1.5.7', 'console_scripts', 'pserve')()
File "/home/cloud/Code/python/3/moo/env/local/lib/python2.7/site-packages/pyramid/scripts/pserve.py", line 58, in main
return command.run()
File "/home/cloud/Code/python/3/moo/env/local/lib/python2.7/site-packages/pyramid/scripts/pserve.py", line 328, in run
global_conf=vars)
File "/home/cloud/Code/python/3/moo/env/local/lib/python2.7/site-packages/pyramid/scripts/pserve.py", line 363, in loadapp
return loadapp(app_spec, name=name, relative_to=relative_to, **kw)
File "/home/cloud/Code/python/3/moo/env/local/lib/python2.7/site-packages/paste/deploy/loadwsgi.py", line 247, in loadapp
return loadobj(APP, uri, name=name, **kw)
File "/home/cloud/Code/python/3/moo/env/local/lib/python2.7/site-packages/paste/deploy/loadwsgi.py", line 272, in loadobj
return context.create()
File "/home/cloud/Code/python/3/moo/env/local/lib/python2.7/site-packages/paste/deploy/loadwsgi.py", line 710, in create
return self.object_type.invoke(self)
File "/home/cloud/Code/python/3/moo/env/local/lib/python2.7/site-packages/paste/deploy/loadwsgi.py", line 146, in invoke
return fix_call(context.object, context.global_conf, **context.local_conf)
File "/home/cloud/Code/python/3/moo/env/local/lib/python2.7/site-packages/paste/deploy/util.py", line 55, in fix_call
val = callable(*args, **kw)
File "/home/cloud/Code/python/3/moo/Moo/moo/__init__.py", line 10, in main
config.add_renderer('.html', 'pyramid.mako_templating.renderer_factory')
File "/home/cloud/Code/python/3/moo/env/local/lib/python2.7/site-packages/pyramid/util.py", line 528, in wrapper
result = wrapped(self, *arg, **kw)
File "/home/cloud/Code/python/3/moo/env/local/lib/python2.7/site-packages/pyramid/config/rendering.py", line 33, in add_renderer
factory = self.maybe_dotted(factory)
File "/home/cloud/Code/python/3/moo/env/local/lib/python2.7/site-packages/pyramid/config/__init__.py", line 825, in maybe_dotted
return self.name_resolver.maybe_resolve(dotted)
File "/home/cloud/Code/python/3/moo/env/local/lib/python2.7/site-packages/pyramid/path.py", line 320, in maybe_resolve
return self._resolve(dotted, package)
File "/home/cloud/Code/python/3/moo/env/local/lib/python2.7/site-packages/pyramid/path.py", line 327, in _resolve
return self._zope_dottedname_style(dotted, package)
File "/home/cloud/Code/python/3/moo/env/local/lib/python2.7/site-packages/pyramid/path.py", line 382, in _zope_dottedname_style
__import__(used)
ImportError: No module named mako_templating
The easiest way to register a new extension to be processed as a Mako template in Pyramid is as follows:
config.include('pyramid_mako')
config.add_mako_renderer('.html')
This will set up the renderer correctly.
Mako templating was removed from the Pyramid core framework, and thus pyramid.mako_templating.* no longer exists.
Depending on how you installed pyramid, you also need to install pyramid_mako, for instance by running pip install pyramid_mako. It's a separate module and is not a part of pyramid itself.
I am unable to access dashboard/admin interface of Google app engine on local development environment which could be access using localhost:8000/_ah/admin/.
It throws following error:
Traceback (most recent call last):
File "C:\Program Files\Google\google_appengine\google\appengine\tools\dev_appserver.py", line 2726, in _HandleRequest
self._Dispatch(dispatcher, self.rfile, outfile, env_dict)
File "C:\Program Files\Google\google_appengine\google\appengine\tools\dev_appserver.py", line 2611, in _Dispatch
base_env_dict=env_dict)
File "C:\Program Files\Google\google_appengine\google\appengine\tools\dev_appserver.py", line 653, in Dispatch
base_env_dict=base_env_dict)
File "C:\Program Files\Google\google_appengine\google\appengine\tools\dev_appserver.py", line 1675, in Dispatch
self._module_dict)
File "C:\Program Files\Google\google_appengine\google\appengine\tools\dev_appserver.py", line 1575, in ExecuteCGI
reset_modules = exec_script(config, handler_path, cgi_path, hook)
File "C:\Program Files\Google\google_appengine\google\appengine\tools\dev_appserver.py", line 1277, in ExecuteOrImportScript
exec module_code in script_module.__dict__
File "C:\Program Files\Google\google_appengine\google\appengine\ext\admin\__init__.py", line 94, in <module>
class _AhAdminXsrfToken_(db.Model):
File "C:\Projects_2013_05_14\retail\common\appenginepatch\appenginepatcher\patch.py", line 539, in __init__
_initialize_model(cls, bases)
File "C:\Projects_2013_05_14\retail\common\appenginepatch\appenginepatcher\patch.py", line 523, in _initialize_model
cls._meta = _meta(cls, bases)
File "C:\Projects_2013_05_14\retail\common\appenginepatch\appenginepatcher\patch.py", line 246, in __init__
raise ValueError('Django expects models (here: %s.%s) to be defined in their own apps!' % (model.__module__, model.__name__))
ValueError: Django expects models (here: __main__._AhAdminXsrfToken_) to be defined in their own apps!
My development environment is as follows.
Google App Engine 1.6.1
Python 2.5
Google app engine patch for Django 1.1
I am unable to understand what is going wrong over here.
Appreciate your help.
I'm in the process of switching an application over from Python 2.5 to 2.7 and have begun encountering a problem with the images service. For example, saving this entity using db.put():
from google.appengine.api import images
class Images(db.Expando):
ImageTitle = db.StringProperty()
ImageFile = blobstore.BlobReferenceProperty()
ImageReference = db.StringProperty()
def put(self, **kwargs):
if not self.ImageReference:
self.ImageReference = images.get_serving_url(self.ImageFile.key())
super(Images, self).put(**kwargs)
Now yields this error:
Traceback (most recent call last):
File "C:\Program Files (x86)\Google\google_appengine\lib\webapp2\webapp2.py", line 1536, in __call__
rv = self.handle_exception(request, response, e)
File "C:\Program Files (x86)\Google\google_appengine\lib\webapp2\webapp2.py", line 1530, in __call__
rv = self.router.dispatch(request, response)
File "C:\Program Files (x86)\Google\google_appengine\lib\webapp2\webapp2.py", line 1278, in default_dispatcher
return route.handler_adapter(request, response)
File "C:\Program Files (x86)\Google\google_appengine\lib\webapp2\webapp2.py", line 1102, in __call__
return handler.dispatch()
File "C:\Program Files (x86)\Google\google_appengine\lib\webapp2\webapp2.py", line 572, in dispatch
return self.handle_exception(e, self.app.debug)
File "C:\Program Files (x86)\Google\google_appengine\lib\webapp2\webapp2.py", line 570, in dispatch
return method(*args, **kwargs)
File "C:\Users\VB User\Bruha\src\handler_product_page_image.py", line 40, in post
image.put()
File "C:\Users\VB User\Bruha\src\db_models.py", line 56, in put
self.ImageReference = images.get_serving_url(self.ImageFile.key())
File "C:\Program Files (x86)\Google\google_appengine\google\appengine\api\images\__init__.py", line 1792, in get_serving_url
rpc = get_serving_url_async(blob_key, size, crop, secure_url, filename, rpc)
File "C:\Program Files (x86)\Google\google_appengine\google\appengine\api\images\__init__.py", line 1907, in get_serving_url_async
None)
File "C:\Program Files (x86)\Google\google_appengine\google\appengine\api\images\__init__.py", line 1034, in _make_async_call
rpc = create_rpc()
File "C:\Program Files (x86)\Google\google_appengine\google\appengine\api\images\__init__.py", line 1028, in create_rpc
return apiproxy_stub_map.UserRPC("images", deadline, callback)
File "C:\Program Files (x86)\Google\google_appengine\google\appengine\api\apiproxy_stub_map.py", line 405, in __init__
self.__rpc = CreateRPC(service, stubmap)
File "C:\Program Files (x86)\Google\google_appengine\google\appengine\api\apiproxy_stub_map.py", line 69, in CreateRPC
'a CreateRPC method.') % service)
AssertionError: The service "images" doesn't have a CreateRPC method.
Calling the 'execute_transforms' method also yields the same error.
Any help understanding what is going on would be much appreciated.
You are running the dev server, so when you start up check for this message `'Could not initialize images API; you are likely missing '
'the Python "PIL" module. ImportError: %s', e
If you are getting this message then the images service RPC is not being registered (the RegisterStub call in dev_appserver will be failing) and you will get the error you are seeing, because the assertion fails in CreateRPC call.
So check to see if PIL is correctly installed for Python 2.7
I have post here about which I learned later that gae has moved here.
Basically I tried django-nonrel example, replacing its django with django-1.3.1 since I coded my app on it but it gives some basic error like cannot find django.db.utils. Note that I have created symbolic link from django -> Django-1.3.1/django and hope its not the issue.
Bases upon replies if I try to use django-nonrel version in my app similar to sample app I get error as below as if its cannot find sqlite3 backend though it mentions as one option in its choices. Also again utils.py is not found.
--> --> -->
Traceback (most recent call last):
File "c:\Program Files\Google\google_appengine\google\appengine\tools\dev_appserver.py", line 4143, in _HandleRequest
self._Dispatch(dispatcher, self.rfile, outfile, env_dict)
File "c:\Program Files\Google\google_appengine\google\appengine\tools\dev_appserver.py", line 4049, in _Dispatch
base_env_dict=env_dict)
File "c:\Program Files\Google\google_appengine\google\appengine\tools\dev_appserver.py", line 616, in Dispatch
base_env_dict=base_env_dict)
File "c:\Program Files\Google\google_appengine\google\appengine\tools\dev_appserver.py", line 3120, in Dispatch
self._module_dict)
File "c:\Program Files\Google\google_appengine\google\appengine\tools\dev_appserver.py", line 3024, in ExecuteCGI
reset_modules = exec_script(handler_path, cgi_path, hook)
File "c:\Program Files\Google\google_appengine\google\appengine\tools\dev_appserver.py", line 2887, in ExecuteOrImportScript
exec module_code in script_module.__dict__
File "I:\learn\python\event\djangoappengine\main\main.py", line 97, in <module>
main()
File "I:\learn\python\event\djangoappengine\main\main.py", line 56, in real_main
run_wsgi_app(application)
File "c:\Program Files\Google\google_appengine\google\appengine\ext\webapp\util.py", line 98, in run_wsgi_app
run_bare_wsgi_app(add_wsgi_middleware(application))
File "c:\Program Files\Google\google_appengine\google\appengine\ext\webapp\util.py", line 116, in run_bare_wsgi_app
result = application(env, _start_response)
File "I:\learn\python\event\django\core\handlers\wsgi.py", line 265, in __call__
response = self.get_response(request)
File "I:\learn\python\event\django\core\handlers\base.py", line 160, in get_response
response = self.handle_uncaught_exception(request, resolver, sys.exc_info())
File "I:\learn\python\event\django\core\handlers\base.py", line 194, in handle_uncaught_exception
return debug.technical_500_response(request, *exc_info)
File "I:\learn\python\event\django\views\debug.py", line 58, in technical_500_response
html = reporter.get_traceback_html()
File "I:\learn\python\event\django\views\debug.py", line 109, in get_traceback_html
frames = self.get_traceback_frames()
File "I:\learn\python\event\django\views\debug.py", line 228, in get_traceback_frames
pre_context_lineno, pre_context, context_line, post_context = self._get_lines_from_file(filename, lineno, 7, loader, module_name)
File "I:\learn\python\event\django\views\debug.py", line 180, in _get_lines_from_file
source = loader.get_source(module_name)
File "c:\Program Files\Google\google_appengine\google\appengine\tools\dev_appserver.py", line 1538, in Decorate
return func(self, *args, **kwargs)
File "c:\Program Files\Google\google_appengine\google\appengine\tools\dev_appserver.py", line 2518, in get_source
full_path, search_path, submodule = self.GetModuleInfo(fullname)
File "c:\Program Files\Google\google_appengine\google\appengine\tools\dev_appserver.py", line 1538, in Decorate
return func(self, *args, **kwargs)
File "c:\Program Files\Google\google_appengine\google\appengine\tools\dev_appserver.py", line 2482, in GetModuleInfo
submodule, search_path = self.GetParentSearchPath(fullname)
File "c:\Program Files\Google\google_appengine\google\appengine\tools\dev_appserver.py", line 1538, in Decorate
return func(self, *args, **kwargs)
File "c:\Program Files\Google\google_appengine\google\appengine\tools\dev_appserver.py", line 2461, in GetParentSearchPath
parent_package = self.GetParentPackage(fullname)
File "c:\Program Files\Google\google_appengine\google\appengine\tools\dev_appserver.py", line 1538, in Decorate
return func(self, *args, **kwargs)
File "c:\Program Files\Google\google_appengine\google\appengine\tools\dev_appserver.py", line 2437, in GetParentPackage
if self.find_module(fullname) is None:
File "c:\Program Files\Google\google_appengine\google\appengine\tools\dev_appserver.py", line 1538, in Decorate
return func(self, *args, **kwargs)
File "c:\Program Files\Google\google_appengine\google\appengine\tools\dev_appserver.py", line 2045, in find_module
search_path)
File "c:\Program Files\Google\google_appengine\google\appengine\tools\dev_appserver.py", line 1538, in Decorate
return func(self, *args, **kwargs)
File "c:\Program Files\Google\google_appengine\google\appengine\tools\dev_appserver.py", line 2375, in FindAndLoadModule
description)
File "c:\Program Files\Google\google_appengine\google\appengine\tools\dev_appserver.py", line 1538, in Decorate
return func(self, *args, **kwargs)
File "c:\Program Files\Google\google_appengine\google\appengine\tools\dev_appserver.py", line 2318, in LoadModuleRestricted
description)
File "I:\learn\python\event\django\db\__init__.py", line 77, in <module>
connection = connections[DEFAULT_DB_ALIAS]
File "I:\learn\python\event\django\db\utils.py", line 91, in __getitem__
backend = load_backend(db['ENGINE'])
File "I:\learn\python\event\django\db\utils.py", line 49, in load_backend
raise ImproperlyConfigured(error_msg)
ImproperlyConfigured: 'django.db.backends.sqlite3' isn't an available database backend.
Try using django.db.backends.XXX, where XXX is one of:
'dummy', 'mysql', 'oracle', 'postgresql', 'postgresql_psycopg2', 'sqlite3'
Error was: cannot import name utils
what version of django is compatible with django-nonrel or do I have to do development using django-nonrel instead of Django-1.3.1 ?
As I read more on django-nonrel I feel that there are few modification needed to django code. Its not straight forward to just use the project as it is. I need to create abstract model super class and it does not support manytomanyfield which I use in my project.
I guess some one confirm these restriction then probably I can close this thread.
March 28
I removed M2M from my app. It was very minor thing just commented out M2M field.
Now I have installed python2.7.2, gae 1.6.4 and djangoappengine gives error as below:
E:\learn\python\event>python manage.py runserver
WARNING:root:E:\software\Google\google_appengine\lib\cacerts\urlfetch_cacerts.tx
t missing; without this urlfetch will not be able to validate SSL certificates.
Traceback (most recent call last):
File "manage.py", line 4, in <module>
import settings # Assumed to be in the same directory.
File "E:\learn\python\event\settings.py", line 4, in <module>
from djangoappengine.settings_base import *
File "E:\learn\python\event\djangoappengine\settings_base.py", line 6, in <mod
ule>
setup_env()
File "E:\learn\python\event\djangoappengine\boot.py", line 69, in setup_env
setup_project()
File "E:\learn\python\event\djangoappengine\boot.py", line 123, in setup_proje
ct
from .utils import have_appserver, on_production_server
File "E:\learn\python\event\djangoappengine\utils.py", line 14, in <module>
default_partition='dev')[0]
TypeError: LoadAppConfig() got an unexpected keyword argument 'default_partition
'
Regards,
Miten.
You can't just replace the version of Django in nonrel. It's actually a fork of Django, with a number of customizations.
Django-1.3.1/Django's database models do not work on Google App Engine.
Django-nonrel implements a backend so that Django models will work on Google App Engine.
So if you want to use Django models, you must use Django-nonrel. Otherwise, you can use Django for templating/forms, but you need to use Google App Engine's db.Model or ndb.Model based models.
My code worked fine when it was all in one file. Now, I'm splitting up classes into different modules. The modules have been given the same name as the classes. Perhaps this is a problem, because MainPage is failing when it is loaded. Does it think that I'm trying to inherit from a module? Can module/class namespace collisions happen?
MainPage.py
import BaseHandler
from models import Item
from Utils import render
class MainPage(BaseHandler):
def body(self, CSIN=None): ##UnusedVariable
self.header('Store')
items = Item.all().order('name').fetch(10)
render('Views/table.html', self, {'items': items})
self.footer()
BaseHandler.py
from google.appengine.ext import webapp
from google.appengine.api import users
from Utils import *
# Controller
class BaseHandler(webapp.RequestHandler):
# ... continues ...
Failure traceback:
Traceback (most recent call last):
File "C:\Program Files\Google\google_appengine\google\appengine\tools\dev_appserver.py", line 3180, in _HandleRequest
self._Dispatch(dispatcher, self.rfile, outfile, env_dict)
File "C:\Program Files\Google\google_appengine\google\appengine\tools\dev_appserver.py", line 3123, in _Dispatch
base_env_dict=env_dict)
File "C:\Program Files\Google\google_appengine\google\appengine\tools\dev_appserver.py", line 515, in Dispatch
base_env_dict=base_env_dict)
File "C:\Program Files\Google\google_appengine\google\appengine\tools\dev_appserver.py", line 2382, in Dispatch
self._module_dict)
File "C:\Program Files\Google\google_appengine\google\appengine\tools\dev_appserver.py", line 2292, in ExecuteCGI
reset_modules = exec_script(handler_path, cgi_path, hook)
File "C:\Program Files\Google\google_appengine\google\appengine\tools\dev_appserver.py", line 2188, in ExecuteOrImportScript
exec module_code in script_module.__dict__
File "C:\Users\odp\workspace\Store\src\Main.py", line 5, in <module>
import MainPage
File "C:\Program Files\Google\google_appengine\google\appengine\tools\dev_appserver.py", line 1267, in Decorate
return func(self, *args, **kwargs)
File "C:\Program Files\Google\google_appengine\google\appengine\tools\dev_appserver.py", line 1917, in load_module
return self.FindAndLoadModule(submodule, fullname, search_path)
File "C:\Program Files\Google\google_appengine\google\appengine\tools\dev_appserver.py", line 1267, in Decorate
return func(self, *args, **kwargs)
File "C:\Program Files\Google\google_appengine\google\appengine\tools\dev_appserver.py", line 1819, in FindAndLoadModule
description)
File "C:\Program Files\Google\google_appengine\google\appengine\tools\dev_appserver.py", line 1267, in Decorate
return func(self, *args, **kwargs)
File "C:\Program Files\Google\google_appengine\google\appengine\tools\dev_appserver.py", line 1770, in LoadModuleRestricted
description)
File "C:\Users\odp\workspace\Store\src\MainPage.py", line 10, in <module>
class MainPage(BaseHandler):
TypeError: Error when calling the metaclass bases
module.__init__() takes at most 2 arguments (3 given)
It appears this can be solved by using
from BaseHandler import BaseHandler
Is it bad style to have the module and class name be the same?
Yes, module names share the same namespace as everything else, and, yes, Python thinks you are trying to inherit from a module.
Change:
class MainPage(BaseHandler):
to:
class MainPage(BaseHandler.BaseHandler):
and you should be good to go. That way, you're saying "please inherit from the BaseHandler class in the BaseHandler module".
Alternately, you could change:
import BaseHandler
to:
from BaseHandler import BaseHandler
First of all the filenames should be all lowercase. That's Python convention that helps to avoid confusion such as this, at least most of the time.
Next, your import from withing MainHandler.py is wrong. You are importing BaseHandler (the module) and referencing it as if it were a class. The class is actually BaseHandler.BaseHandler. You need to reference it as such.
Try that and it should work for you.