Pylons / pyramid_jinja2

Jinja2 templating system bindings for the Pyramid web framework
https://docs.pylonsproject.org/projects/pyramid-jinja2/en/latest/
Other
71 stars 57 forks source link

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

Open katzlbt opened 11 years ago

katzlbt commented 11 years ago

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 commented 11 years ago

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 commented 10 years ago

Also relevant: https://github.com/docent/pyramid_jinja2/commit/a6f1b7929358b7446ba60a9f745599bf6da9443d

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

domenkozar commented 10 years ago

Note: this will bump required pyramid version to 1.3

domenkozar commented 9 years ago

Closing this, let me know if bug still exists

jer-tx commented 7 years ago

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

katzlbt commented 7 years ago

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