Classname same as file/module name leads to inheritance issue - python

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.

Related

Django 1.7: Lazy evaluation to avoid "App registry isn't ready yet" in models.py

I have a query in a model's method definition:
'content_type_id': ContentType.objects.get_for_model(model).pk,
https://github.com/seperman/django-tagging/blob/develop/tagging/models.py#L107
And it raises "App registry is not ready yet".
Putting the line inside another independent function doesn't help either since it still gets called.
Django docs:
Executing database queries with the ORM at import time in models
modules will also trigger this exception. The ORM cannot function
properly until all models are available.
How can I make it lazy evaluate this line?
I thought about caching its results manually in a file by making a management command. And then reading the cached result but there should be a much better/cleaner solution(?)
./manage.py shell
Traceback (most recent call last):
File "./manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "/var/www/something.com/something/shared/env/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 385, in execute_from_command_line
utility.execute()
File "/var/www/something.com/something/shared/env/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 354, in execute
django.setup()
File "/var/www/something.com/something/shared/env/local/lib/python2.7/site-packages/django/__init__.py", line 21, in setup
apps.populate(settings.INSTALLED_APPS)
File "/var/www/something.com/something/shared/env/local/lib/python2.7/site-packages/django/apps/registry.py", line 108, in populate
app_config.import_models(all_models)
File "/var/www/something.com/something/shared/env/local/lib/python2.7/site-packages/django/apps/config.py", line 202, in import_models
self.models_module = import_module(models_module_name)
File "/usr/lib/python2.7/importlib/__init__.py", line 37, in import_module
__import__(name)
File "/var/www/something.com/something/current/apps/riding/models.py", line 32, in <module>
class RidingSection(CategoryBase):
File "/var/www/something.com/something/shared/env/local/lib/python2.7/site-packages/mptt/models.py", line 244, in __new__
cls = meta.register(cls)
File "/var/www/something.com/something/shared/env/local/lib/python2.7/site-packages/mptt/models.py", line 325, in register
obj = getattr(cls, attr)
File "/var/www/something.com/something/current/apps/tagging/fields.py", line 54, in __get__
return edit_string_for_tags(Tag.objects.usage_for_model(owner))
File "/var/www/something.com/something/current/apps/tagging/models.py", line 148, in usage_for_model
usage = self.usage_for_queryset(queryset, counts, min_count)
File "/var/www/something.com/something/current/apps/tagging/models.py", line 185, in usage_for_queryset
return self._get_usage(queryset.model, counts, min_count, extra_joins, extra_criteria, params)
File "/var/www/something.com/something/current/apps/tagging/models.py", line 107, in _get_usage
'content_type_id': ContentType.objects.get_for_model(model).pk,
File "/var/www/something.com/something/shared/env/local/lib/python2.7/site-packages/django/contrib/contenttypes/models.py", line 50, in get_for_model
defaults={'name': smart_text(opts.verbose_name_raw)},
File "/var/www/something.com/something/shared/env/local/lib/python2.7/site-packages/django/db/models/manager.py", line 92, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "/var/www/something.com/something/shared/env/local/lib/python2.7/site-packages/django/db/models/query.py", line 422, in get_or_create
return self.get(**lookup), False
File "/var/www/something.com/something/shared/env/local/lib/python2.7/site-packages/django/db/models/query.py", line 345, in get
clone = self.filter(*args, **kwargs)
File "/var/www/something.com/something/shared/env/local/lib/python2.7/site-packages/django/db/models/query.py", line 691, in filter
return self._filter_or_exclude(False, *args, **kwargs)
File "/var/www/something.com/something/shared/env/local/lib/python2.7/site-packages/django/db/models/query.py", line 709, in _filter_or_exclude
clone.query.add_q(Q(*args, **kwargs))
File "/var/www/something.com/something/shared/env/local/lib/python2.7/site-packages/django/db/models/sql/query.py", line 1287, in add_q
clause, require_inner = self._add_q(where_part, self.used_aliases)
File "/var/www/something.com/something/shared/env/local/lib/python2.7/site-packages/django/db/models/sql/query.py", line 1314, in _add_q
current_negated=current_negated, connector=connector)
File "/var/www/something.com/something/shared/env/local/lib/python2.7/site-packages/django/db/models/sql/query.py", line 1138, in build_filter
lookups, parts, reffed_aggregate = self.solve_lookup_type(arg)
File "/var/www/something.com/something/shared/env/local/lib/python2.7/site-packages/django/db/models/sql/query.py", line 1076, in solve_lookup_type
_, field, _, lookup_parts = self.names_to_path(lookup_splitted, self.get_meta())
File "/var/www/something.com/something/shared/env/local/lib/python2.7/site-packages/django/db/models/sql/query.py", line 1339, in names_to_path
field, model, direct, m2m = opts.get_field_by_name(name)
File "/var/www/something.com/something/shared/env/local/lib/python2.7/site-packages/django/db/models/options.py", line 416, in get_field_by_name
cache = self.init_name_map()
File "/var/www/something.com/something/shared/env/local/lib/python2.7/site-packages/django/db/models/options.py", line 445, in init_name_map
for f, model in self.get_all_related_m2m_objects_with_model():
File "/var/www/something.com/something/shared/env/local/lib/python2.7/site-packages/django/db/models/options.py", line 563, in get_all_related_m2m_objects_with_model
cache = self._fill_related_many_to_many_cache()
File "/var/www/something.com/something/shared/env/local/lib/python2.7/site-packages/django/db/models/options.py", line 577, in _fill_related_many_to_many_cache
for klass in self.apps.get_models():
File "/var/www/something.com/something/shared/env/local/lib/python2.7/site-packages/django/utils/lru_cache.py", line 101, in wrapper
result = user_function(*args, **kwds)
File "/var/www/something.com/something/shared/env/local/lib/python2.7/site-packages/django/apps/registry.py", line 168, in get_models
self.check_models_ready()
File "/var/www/something.com/something/shared/env/local/lib/python2.7/site-packages/django/apps/registry.py", line 131, in check_models_ready
raise AppRegistryNotReady("Models aren't loaded yet.")
django.core.exceptions.AppRegistryNotReady: Models aren't loaded yet.
django-tagging is being a little inconsiderate here.
TagField is a descriptor, meaning if you access YourModel.tags, it runs code which executes a database query.
It just so happens that django-mptt iterates over all the attributes of your MPTTModel classes during startup (before the app cache has loaded), so it happens to trigger this exception.
I don't know how you'd prevent that in django-tagging while maintaining the API (YourModel.tags has to get tags from somewhere). Issues like this are probably the reason why django-tagging is no longer maintained.
However, I'm the maintainer of django-mptt and I've committed a workaround. If you upgrade to the master version of mptt you should find it works now.
You are calling _get_usage before the app has been registered. Wherever you are calling it needs to be changed to a point where the app has been configured. Basically, if you need a model at import time you should use ready().
For example, if you had a signals.py you could do:
# in apps.py
class SomeAppConfig(AppConfig):
def ready(self):
from . import signals
or in your case:
# in apps.py
class SomeAppConfig(AppConfig):
def ready(self):
mgr = TagManager()
mgr._get_usage(#some_args)
As the docs state: ready() ... is called as soon as the registry is fully populated

Cannot import ndb

i've just approached Google App Engine. I've joined a project without having a clue about how it work so i'm sorry if this question looks dumb.
My problem is that looking at the log, it says it cannot import ndb. I've looked up for that a bit and discovered this should be present by default in version 1.6.4 of the google app engine.
I've tried to import it from console and it doesn't work either. My installation of python and google app engine are clean, i did both today.
What should i do?
I'm working on windows with python 2.7 and google app engine 1.6.4.
This is the line of code giving me problems:
from wtforms.ext.appengine.ndb import model_form
Here is the error:
INFO 2012-04-10 19:44:34,582 dev_appserver.py:2884] "GET /
HTTP/1.1" 500 - ERROR 2012-04-10 19:47:05,239 wsgi.py:189]
Traceback (most recent call last): File "C:\Program Files
(x86)\Google\google_appengine\google\appengine\runtime\wsgi.py", line
187, in Handle
handler = _config_handle.add_wsgi_middleware(self._LoadHandler()) File "C:\Program Files
(x86)\Google\google_appengine\google\appengine\runtime\wsgi.py", line
236, in _LoadHandler
import(cumulative_path) File "C:\Program Files (x86)\Google\google_appengine\google\appengine\tools\dev_appserver_import_hook.py",
line 676, in Decorate
return func(self, *args, **kwargs) File "C:\Program Files (x86)\Google\google_appengine\google\appengine\tools\dev_appserver_import_hook.py",
line 1850, in load_module
return self.FindAndLoadModule(submodule, fullname, search_path) File "C:\Program Files
(x86)\Google\google_appengine\google\appengine\tools\dev_appserver_import_hook.py",
line 676, in Decorate
return func(self, *args, **kwargs) File "C:\Program Files (x86)\Google\google_appengine\google\appengine\tools\dev_appserver_import_hook.py",
line 1722, in FindAndLoadModule
description) File "C:\Program Files (x86)\Google\google_appengine\google\appengine\tools\dev_appserver_import_hook.py",
line 676, in Decorate
return func(self, *args, **kwargs) File "C:\Program Files (x86)\Google\google_appengine\google\appengine\tools\dev_appserver_import_hook.py",
line 1665, in LoadModuleRestricted
description) File "C:\Users\chobeat\workspace\pappa Mi\py\main.py", line 38, in
from py.comments import * File "C:\Program Files (x86)\Google\google_appengine\google\appengine\tools\dev_appserver_import_hook.py",
line 676, in Decorate
return func(self, *args, **kwargs) File "C:\Program Files (x86)\Google\google_appengine\google\appengine\tools\dev_appserver_import_hook.py",
line 1850, in load_module
return self.FindAndLoadModule(submodule, fullname, search_path) File "C:\Program Files
(x86)\Google\google_appengine\google\appengine\tools\dev_appserver_import_hook.py",
line 676, in Decorate
return func(self, *args, **kwargs) File "C:\Program Files (x86)\Google\google_appengine\google\appengine\tools\dev_appserver_import_hook.py",
line 1722, in FindAndLoadModule
description) File "C:\Program Files (x86)\Google\google_appengine\google\appengine\tools\dev_appserver_import_hook.py",
line 676, in Decorate
return func(self, *args, **kwargs) File "C:\Program Files (x86)\Google\google_appengine\google\appengine\tools\dev_appserver_import_hook.py",
line 1665, in LoadModuleRestricted
description) File "C:\Users\chobeat\workspace\pappa Mi\py\comments.py", line 20, in
from py.form import IspezioneForm, NonconformitaForm, DietaForm, NotaForm File "C:\Program Files
(x86)\Google\google_appengine\google\appengine\tools\dev_appserver_import_hook.py",
line 676, in Decorate
return func(self, *args, **kwargs) File "C:\Program Files (x86)\Google\google_appengine\google\appengine\tools\dev_appserver_import_hook.py",
line 1850, in load_module
return self.FindAndLoadModule(submodule, fullname, search_path) File "C:\Program Files
(x86)\Google\google_appengine\google\appengine\tools\dev_appserver_import_hook.py",
line 676, in Decorate
return func(self, *args, **kwargs) File "C:\Program Files (x86)\Google\google_appengine\google\appengine\tools\dev_appserver_import_hook.py",
line 1722, in FindAndLoadModule
description) File "C:\Program Files (x86)\Google\google_appengine\google\appengine\tools\dev_appserver_import_hook.py",
line 676, in Decorate
return func(self, *args, **kwargs) File "C:\Program Files (x86)\Google\google_appengine\google\appengine\tools\dev_appserver_import_hook.py",
line 1665, in LoadModuleRestricted
description) File "C:\Users\chobeat\workspace\pappa Mi\py\form.py", line 6, in
from wtforms.ext.appengine.ndb import model_form ImportError: No module named ndb
You need to import it like this:
from google.appengine.ext import ndb
If you just say import ndb you are missing out on some import path information. Don't be afraid to copy the code that is having trouble into your question. Often, that will provide the vital clue.
UPDATE:
WTForms is not included as part of the AppEngine SDK. You'll need to install it on your system.
from wtforms.ext.appengine.ndb import model_form
can't work. wtforms does not have a ndb package.
it should be:
from wtforms.ext.appengine.db import model_form

Running Pure Django Projects on Google App Engine - sample zip - django-1.3.1

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.

How do I properly install httplib2 on Google App Engine?

I copied my httplib2 directory into my GAE project, and now I'm getting the following error:
line 64, in <module> _ssl_wrap_socket = ssl.wrap_socket
Is this an issue with Google App Engine, or did I somehow install my httplib2 incorrectly?
Full error dump, as request(it's big!):
--> --> -->
Traceback (most recent call last):
File "C:\Program Files (x86)\Google\google_appengine\google\appengine\tools\dev_appserver.py", line 4053, in _HandleRequest
self._Dispatch(dispatcher, self.rfile, outfile, env_dict)
File "C:\Program Files (x86)\Google\google_appengine\google\appengine\tools\dev_appserver.py", line 3977, in _Dispatch
base_env_dict=env_dict)
File "C:\Program Files (x86)\Google\google_appengine\google\appengine\tools\dev_appserver.py", line 588, in Dispatch
base_env_dict=base_env_dict)
File "C:\Program Files (x86)\Google\google_appengine\google\appengine\tools\dev_appserver.py", line 3050, in Dispatch
self._module_dict)
File "C:\Program Files (x86)\Google\google_appengine\google\appengine\tools\dev_appserver.py", line 2954, in ExecuteCGI
reset_modules = exec_script(handler_path, cgi_path, hook)
File "C:\Program Files (x86)\Google\google_appengine\google\appengine\tools\dev_appserver.py", line 2834, in ExecuteOrImportScript
exec module_code in script_module.__dict__
File "C:\Users\quaunaut\Documents\Aptana Studio 3 Workspace\qushoutout\src\main.py", line 25, in <module>
import twitter as twitter
File "C:\Program Files (x86)\Google\google_appengine\google\appengine\tools\dev_appserver.py", line 1505, in Decorate
return func(self, *args, **kwargs)
File "C:\Program Files (x86)\Google\google_appengine\google\appengine\tools\dev_appserver.py", line 2450, in load_module
return self.FindAndLoadModule(submodule, fullname, search_path)
File "C:\Program Files (x86)\Google\google_appengine\google\appengine\tools\dev_appserver.py", line 1505, in Decorate
return func(self, *args, **kwargs)
File "C:\Program Files (x86)\Google\google_appengine\google\appengine\tools\dev_appserver.py", line 2339, in FindAndLoadModule
description)
File "C:\Program Files (x86)\Google\google_appengine\google\appengine\tools\dev_appserver.py", line 1505, in Decorate
return func(self, *args, **kwargs)
File "C:\Program Files (x86)\Google\google_appengine\google\appengine\tools\dev_appserver.py", line 2282, in LoadModuleRestricted
description)
File "C:\Users\quaunaut\Documents\Aptana Studio 3 Workspace\qushoutout\src\twitter.py", line 65, in <module>
import oauth2 as oauth
File "C:\Program Files (x86)\Google\google_appengine\google\appengine\tools\dev_appserver.py", line 1505, in Decorate
return func(self, *args, **kwargs)
File "C:\Program Files (x86)\Google\google_appengine\google\appengine\tools\dev_appserver.py", line 2450, in load_module
return self.FindAndLoadModule(submodule, fullname, search_path)
File "C:\Program Files (x86)\Google\google_appengine\google\appengine\tools\dev_appserver.py", line 1505, in Decorate
return func(self, *args, **kwargs)
File "C:\Program Files (x86)\Google\google_appengine\google\appengine\tools\dev_appserver.py", line 2339, in FindAndLoadModule
description)
File "C:\Program Files (x86)\Google\google_appengine\google\appengine\tools\dev_appserver.py", line 1505, in Decorate
return func(self, *args, **kwargs)
File "C:\Program Files (x86)\Google\google_appengine\google\appengine\tools\dev_appserver.py", line 2282, in LoadModuleRestricted
description)
File "C:\Users\quaunaut\Documents\Aptana Studio 3 Workspace\qushoutout\src\oauth2\__init__.py", line 32, in <module>
import httplib2
File "C:\Program Files (x86)\Google\google_appengine\google\appengine\tools\dev_appserver.py", line 1505, in Decorate
return func(self, *args, **kwargs)
File "C:\Program Files (x86)\Google\google_appengine\google\appengine\tools\dev_appserver.py", line 2450, in load_module
return self.FindAndLoadModule(submodule, fullname, search_path)
File "C:\Program Files (x86)\Google\google_appengine\google\appengine\tools\dev_appserver.py", line 1505, in Decorate
return func(self, *args, **kwargs)
File "C:\Program Files (x86)\Google\google_appengine\google\appengine\tools\dev_appserver.py", line 2339, in FindAndLoadModule
description)
File "C:\Program Files (x86)\Google\google_appengine\google\appengine\tools\dev_appserver.py", line 1505, in Decorate
return func(self, *args, **kwargs)
File "C:\Program Files (x86)\Google\google_appengine\google\appengine\tools\dev_appserver.py", line 2282, in LoadModuleRestricted
description)
File "C:\Users\quaunaut\Documents\Aptana Studio 3 Workspace\qushoutout\src\httplib2\__init__.py", line 64, in <module>
_ssl_wrap_socket = ssl.wrap_socket
AttributeError: 'module' object has no attribute 'wrap_socket'
Still having this issue. About the only thing I can think of that could possibly be causing this is my machine running Python 2.7; Is it possible that is causing the issue?
As a workaround:
Can you use Google App Engine SDK 1.4.3 (this looks like a 1.5.0 regression).
Or force httplib2 fallback on httplib.FakeSocket with:
import sys
sys.modules['ssl'] = None
import httplib2
A new issue has been filled there:
http://code.google.com/p/googleappengine/issues/detail?id=5064
It looks already fixed in httplib2 side:
http://code.google.com/p/httplib2/source/detail?r=cf721c1693a68e9438899be3d78acccae6ab0e30#
The AppEngine sandbox doesn't allow access to a sockets API, for an http library to work on GAE it would have to wrap urllib (which in turn wraps the url_fetch API).
EDIT Looks like httplib2 should work so I guess you just need to ensure your paths are setup correctly.
See this question
This Blog claims to fix the GAE issues. I haven't tried it yet, but sounds right.

Python: NameError: 'self' is not defined

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.

Categories