【问题标题】:GAE Python dev server crashes intermittently after upgrade to 2.7升级到 2.7 后,GAE Python 开发服务器间歇性崩溃
【发布时间】:2012-01-21 21:10:50
【问题描述】:

我最近将我的 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。
  • 我正在使用带有 Lion 的 Mac。

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

【问题讨论】:

  • 最新的 Python 和开发服务器仍然存在同样的问题。

标签: python google-app-engine


【解决方案1】:

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

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

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

查看堆栈跟踪的第 4 行,其中 pytz 被导入,下一行来自 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。

干杯

【讨论】:

  • 嗯...我没有在我的应用程序中使用 setuptools,所以我不知道它被导入的原因和位置。
  • 查看导入 pytz 的堆栈跟踪的第 4 行,下一行是 from pkg_resources import resource_stream 触发其余导入问题的原因。我在项目的根目录中使用了一个假的截断 pkg_resources,它最终不会尝试从设置工具中导入东西。您可以在调试导入模式下运行开发服务器,这会告诉您更多信息。
  • 谢谢!这看起来真的很有帮助。在 gae-pytz 的 __init__.py 文件中将 from pkg_resources import resource_stream 注释掉会更容易吗?
  • 如果您有任何其他 egg 部署包,您将需要它。这样你就不需要接触其他库了。
  • 我只会修改gae-pytz,所以我认为它不会影响任何其他鸡蛋。
【解决方案2】:

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

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

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

我把前三行注释掉了,问题就解决了。

【讨论】:

    【解决方案3】:

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

    【讨论】:

      猜你喜欢
      • 2016-01-05
      • 2021-02-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-06-14
      • 2012-11-21
      相关资源
      最近更新 更多