GAE Python dev服务器在升级到2.7后间歇性地崩溃

时间:2012-01-21 21:10:51

标签: python google-app-engine

我最近将我的GAE Python应用程序升级到Python 2.7。从那时起,我定期与开发服务器发生以下错误,并且开发服务器提供一个空白页面:

Traceback (most recent call last):
  File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/runtime/wsgi.py", line 168, in Handle
    handler = _config_handle.add_wsgi_middleware(self._LoadHandler())
  File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/runtime/wsgi.py", line 206, in _LoadHandler
    handler = __import__(path[0])
  [...]
  File "/Users/joneill/OpenSTV/OpenSTV/trunk/OpaVote-HR/main.py", line 2, in <module>
    import views
  [...]
  File "/Users/joneill/OpenSTV/OpenSTV/trunk/OpaVote-HR/views.py", line 3, in <module>
    from pytz.gae import pytz
  [...]
  File "/Users/joneill/OpenSTV/OpenSTV/trunk/OpaVote-HR/pytz/__init__.py", line 34, in <module>
    from pkg_resources import resource_stream
  File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/tools/dev_appserver_import_hook.py", line 662, in Decorate
    return func(self, *args, **kwargs)
  File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/tools/dev_appserver_import_hook.py", line 1818, in load_module
    return self.FindAndLoadModule(submodule, fullname, search_path)
  File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/tools/dev_appserver_import_hook.py", line 662, in Decorate
    return func(self, *args, **kwargs)
  File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/tools/dev_appserver_import_hook.py", line 1690, in FindAndLoadModule
    description)
  File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/tools/dev_appserver_import_hook.py", line 662, in Decorate
    return func(self, *args, **kwargs)
  File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/tools/dev_appserver_import_hook.py", line 1615, in LoadModuleRestricted
    return source_file.load_module(submodule_fullname)
  File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/dist/py_zipimport.py", line 246, in load_module
    submodname, is_package, fullpath, source = self._get_source(fullmodname)
  File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/dist/py_zipimport.py", line 207, in _get_source
    source = self.zipfile.read(relpath.replace(os.sep, '/'))
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/zipfile.py", line 867, in read
    return self.open(name, "r", pwd).read()
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/zipfile.py", line 882, in open
    zef_file = open(self.filename, 'rb')
  File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/tools/dev_appserver_import_hook.py", line 578, in __init__
    raise IOError(errno.EACCES, 'file not accessible', filename)
IOError: [Errno 13] file not accessible: '/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/setuptools-0.6c11-py2.7.egg'
INFO     2012-01-21 20:50:44,222 dev_appserver.py:2832] "POST /manage HTTP/1.1" 500 -

一些注意事项:

  • 生产服务器上不会发生这种情况。
  • 在开发服务器上,我的应用程序将运行几分钟,然后发生此错误。
  • 如果我在开发服务器上停止并重启我的应用程序,它将再次运行几分钟。
  • 我使用的是gae-pytz的最新版本,您可以看到它在导入时失败。
  • 我删除的内容与您在近端看到的内容类似。
  • 我不知道为什么最后会调用setuptools。
  • 我正在使用带狮子的Mac。

我可以使用开发服务器,但每隔几分钟停止并重新启动真的很烦人。任何想法如何解决这个问题?

3 个答案:

答案 0 :(得分:1)

堆栈跟踪的实际问题是,您的代码正试图从站点包导入设置工具,开发服务器不会这样做。

&#39; /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/setuptools-0.6c11-py2.7.egg'

您需要在应用程序代码库中包含setuptools。它有时工作的事实表明,您通过各种模块编写路径的方式各不相同,并且可能(取决于您在开发中的测试)不同的导入顺序意味着设置工具已在其他地方导入,或者仅在代码中的某些点处需要

看一下导入pytz的堆栈跟踪的第4行,下一行来自pkg_resources import resource_stream,它们触发了其余的导入问题。我在项目的根目录中使用了一个假的截断的pkg_resources,它最终没有尝试从设置工具中导入东西。您可以在调试导入模式下运行开发服务器,这将告诉您更多

这是假的pkg_resources。

"""Package resource API
--------------------

A resource is a logical file contained within a package, or a logical
subdirectory thereof.  The package resource API expects resource names
to have their path parts separated with ``/``, *not* whatever the local
path separator is.  Do not use os.path operations to manipulate resource
names being passed into the API.

The package resource API is designed to work with normal filesystem packages,
.egg files, and unpacked .egg files.  It can also work in a limited way with
.zip files and with custom PEP 302 loaders that support the ``get_data()``
method.
"""

import sys, os, zipimport, time, re, imp, new

try:
    frozenset
except NameError:
   from sets import ImmutableSet as frozenset

from os import utime   #, rename, unlink    # capture these to bypass sandboxing
from os import open as os_open

可能还有其他/更好的方法,但这对我有用。

哦,我还建议您使用http://code.google.com/p/gae-pytz/而不是pytz。

干杯

答案 1 :(得分:0)

我更喜欢上述的替代答案。

pytz的__init__.py文件包含以下行:

#try:
#    from pkg_resources import resource_stream
#except ImportError:
resource_stream = None

我评论了前三行并解决了问题。

答案 2 :(得分:0)

问题是使用Python 2.7的App Engine开发服务器中的错误。解决方案在这里: File not accesible error (setuptools) in logs