pyramid_jinja2
pyramid_jinja2 copied to clipboard
Google Appengine: KeyError: '__main__' when using config.include()
I am using google appengine with pyramid, and tried to use jinja2. When I add config.include('pyramid_jinja2') it crashes in some introspect code ... in _get_or_build_default_environment(config.registry) It seems to crash in python's inspect.py
Someone else had this error too: http://stackoverflow.com/questions/8031476/pyramid-jinja2-and-new-gae-runtime It can be fixed by creating a VirtualModule main which means patching init.py of every new release of pyramid_jinja2, see comment below.
main.py ... import os import logging logging.getLogger().setLevel(logging.DEBUG)
from pyramid.config import Configurator from yaml import load from appglobals import APP_BASE_DIR
SETTINGS_FILE = os.path.join(APP_BASE_DIR, 'settings.yaml')
def app_config(): config = Configurator(settings=load(open(SETTINGS_FILE, 'r').read())) config.add_settings({'locandy.appbasedir': APP_BASE_DIR}) config.hook_zca() config.include('pyramid_jinja2') config.add_route('catchall', '{notfound:.*}') return config
config = app_config()
application = config.make_wsgi_app()
Traceback (most recent call last):
File "/Users/cat/repositories/locandy-web/parts/appengine_sdk/google/appengine/runtime/wsgi.py", line 196, in Handle
handler = _config_handle.add_wsgi_middleware(self._LoadHandler())
File "/Users/cat/repositories/locandy-web/parts/appengine_sdk/google/appengine/runtime/wsgi.py", line 255, in _LoadHandler
handler = import(path[0])
File "/Users/cat/repositories/locandy-web/app/main.py", line 22, in
Here is a hint ... http://www.inductiveautomation.com/forum/viewtopic.php?f=70&p=36917
Solved ... patched the init.py
class VirtualModule(object):
def __init__(self,name):
import sys
sys.modules[name]=self
def __getattr__(self,name):
return globals()[name]
VirtualModule("__main__")
Also relevant: https://github.com/docent/pyramid_jinja2/commit/a6f1b7929358b7446ba60a9f745599bf6da9443d
Hopefully someone on GAE provides a patch with tests someday :-)
Note: this will bump required pyramid version to 1.3
Closing this, let me know if bug still exists
@domenkozar This bug appears to still exist. I'm working on fleshing out Pyramid's support for appengine (at the very least seeing where it stands) and am able to get chameleon working just fine, but jinja2 doesn't seem to work right. Getting this error, where all I'm doing is swapping out Chameleon for Jinja2.
The workaround doesn't appear to be valid anymore, as I'm not seeing a _get_or_build_default_environment function anywhere.
_get_or_build_default_environment is only context to hint where the patch could go in the file, I removed it in my comment because it is confusing.
I just append the code to pyramid_jinja2/init.py using echo statements automatically.