Google Appengine: KeyError: '__main__' when using config.include() #63

Open
katzlbt opened this Issue Aug 6, 2013 · 6 comments

Comments

Projects
None yet
4 participants
@katzlbt

katzlbt commented Aug 6, 2013

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
config = app_config()
File "/Users/cat/repositories/locandy-web/app/main.py", line 18, in app_config
config.include('pyramid_jinja2')
File "distlib/pyramid/config/init.py", line 773, in include
c(configurator)
File "distlib/pyramid_jinja2/init.py", line 468, in includeme
_get_or_build_default_environment(config.registry)
File "distlib/pyramid_jinja2/init.py", line 252, in _get_or_build_default_environment
package = _caller_package(('pyramid_jinja2', 'jinja2', 'pyramid.config'))
File "distlib/pyramid_jinja2/init.py", line 132, in caller_package
for t in self.inspect.stack():
File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/inspect.py", line 1054, in stack
return getouterframes(sys._getframe(1), context)
File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/inspect.py", line 1032, in getouterframes
framelist.append((frame,) + getframeinfo(frame, context))
File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/inspect.py", line 1007, in getframeinfo
lines, lnum = findsource(frame)
File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/inspect.py", line 532, in findsource
module = getmodule(object, file)
File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/inspect.py", line 504, in getmodule
main = sys.modules['main']
KeyError: 'main'

@katzlbt

This comment has been minimized.

Show comment Hide comment
@katzlbt

katzlbt Aug 6, 2013

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__")

katzlbt commented Aug 6, 2013

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__")
@domenkozar

This comment has been minimized.

Show comment Hide comment
@domenkozar

domenkozar Nov 8, 2013

Member

Also relevant: docent/pyramid_jinja2@a6f1b79

Hopefully someone on GAE provides a patch with tests someday :-)

Member

domenkozar commented Nov 8, 2013

Also relevant: docent/pyramid_jinja2@a6f1b79

Hopefully someone on GAE provides a patch with tests someday :-)

@domenkozar

This comment has been minimized.

Show comment Hide comment
@domenkozar

domenkozar Nov 8, 2013

Member

Note: this will bump required pyramid version to 1.3

Member

domenkozar commented Nov 8, 2013

Note: this will bump required pyramid version to 1.3

@domenkozar

This comment has been minimized.

Show comment Hide comment
@domenkozar

domenkozar Apr 16, 2015

Member

Closing this, let me know if bug still exists

Member

domenkozar commented Apr 16, 2015

Closing this, let me know if bug still exists

@domenkozar domenkozar closed this Apr 16, 2015

@Jeremydavisvt

This comment has been minimized.

Show comment Hide comment
@Jeremydavisvt

Jeremydavisvt Jul 10, 2017

@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.

@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.

@mmerickel mmerickel reopened this Jul 10, 2017

@katzlbt

This comment has been minimized.

Show comment Hide comment
@katzlbt

katzlbt Jul 10, 2017

_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.

katzlbt commented Jul 10, 2017

_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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment