python - 如何在 Mac OS X 10.6 上将 virtualenv 与 Google App Engine SDK 一起使用

标签 python macos google-app-engine virtualenv

我正在拔头发试图解决这个问题,因为直到上周我一直在工作,但不知何故它坏了。

当我为 Google App Engine 应用设置 virtualenv 并使用 dev_appserver.py 启动应用时,我在导入标准库时遇到错误(例如“ImportError: No module named base64”)。

这就是我正在做的事情:

(使用系统Python)

virtualenv --python=python2.5 --no-site-packages ~/.virtualenv/foobar

然后我将 gae.pth 文件添加到包含 Google App Engine 库的 ~/.virtualenv/foobar/lib/python2.5/site-packages/ :

/usr/local/google_appengine
/usr/local/google_appengine/lib/antlr3
/usr/local/google_appengine/lib/cacerts
/usr/local/google_appengine/lib/django
/usr/local/google_appengine/lib/fancy_urllib
/usr/local/google_appengine/lib/ipaddr
/usr/local/google_appengine/lib/webob_1_1_1
/usr/local/google_appengine/lib/yaml/lib

(这是基于 this answer 。)

然后我获取我的“foobar”virtualenv 并尝试使用 dev_appserver.py 启动我的应用程序。

服务器启动,但第一个请求出错,出现上述“ImportError: No module named base64”。如果我访问管理控制台,我会收到“ImportError: No module named cgi”。

如果我启动 python,我可以加载这些模块。

>>> import base64
>>> base64.__file__
'/System/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/base64.py'

SDK 的沙盒功能似乎阻止了这些库的加载。但就像我说的那样,我直到上周才开始工作......有些东西发生了变化,或者我无意中破坏了我的 virtualenv,我不知道我是如何让它工作的。

软件版本:

Google App Engine SDK 1.3.7
Mac OS X 雪豹 10.6.4
虚拟环境 1.5.1

更新:针对 Alan Franzoni 的问题:

我正在使用 Mac OS X 附带的系统 Python。我通过 easy_install 安装了 virtualenv。我今天升级到 virtualenv 1.5.1 试图解决这个问题。

如果我使用 virtualenv python 运行 python/usr/local/bin/dev_appserver.py,问题仍然存在。如果我停用 virtualenv 并使用系统 python2.5 运行该命令,它就可以工作。 (另外,我可以使用 GoogleAppEngineLauncher 启动我的应用程序。)

这是一个完整的堆栈跟踪(这个使用了 Kay 框架,但问题与 webapp 相同):

Traceback (most recent call last):
  File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/tools/dev_appserver.py", line 3206, in _HandleRequest
    self._Dispatch(dispatcher, self.rfile, outfile, env_dict)
  File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/tools/dev_appserver.py", line 3149, in _Dispatch
    base_env_dict=env_dict)
  File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/tools/dev_appserver.py", line 525, in Dispatch
    base_env_dict=base_env_dict)
  File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/tools/dev_appserver.py", line 2402, in Dispatch
    self._module_dict)
  File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/tools/dev_appserver.py", line 2312, in ExecuteCGI
    reset_modules = exec_script(handler_path, cgi_path, hook)
  File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/tools/dev_appserver.py", line 2208, in ExecuteOrImportScript
    exec module_code in script_module.__dict__
  File "/Users/look/myapp/kay/main.py", line 17, in <module>
    kay.setup()
  File "/Users/look/myapp/kay/__init__.py", line 122, in setup
    from google.appengine.ext import db
  File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/tools/dev_appserver.py", line 1287, 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.py", line 1937, 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.py", line 1287, 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.py", line 1839, in FindAndLoadModule
    description)
  File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/tools/dev_appserver.py", line 1287, 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.py", line 1790, in LoadModuleRestricted
    description)
  File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/ext/db/__init__.py", line 81, in <module>
    import base64
ImportError: No module named base64

最佳答案

这是一个 issue 4339使用 GAE SDK,它得到了确认,并且在 bug 条目中有两个稍微不同的补丁可以使它工作。

发生的事情是dev_appserver.py通过禁止访问任何非系统 python 模块来设置受限 python 环境,它通过从 os 的位置计算系统 python 文件夹来实现。模块。在 virtualenv 实例中 os.py符号链接(symbolic link)到 virtualenv 但直接编译到 virtualenv,这是 dev_appserver 使用的路径,有效地阻止访问系统 python 库中未由 virtualend 链接的任何模块,这是大部分他们。解决办法是“祝福”两条路。

关于python - 如何在 Mac OS X 10.6 上将 virtualenv 与 Google App Engine SDK 一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3858772/

相关文章:

python - 是否可以在 leftOuterJoin 上初始化一个空的默认值?

Python 在 Apache 日志文件中搜索 IP 数量

python - 并行多维优化

macos - 为什么这段汇编代码会抛出段错误?

macos - OSX Web 浏览器是否有类似 IE9 的 CSS 属性?

java - 如何在本地安装 Java 7 的情况下将 Play Framework 与 Google App Engine 一起使用?

django - 带有 mysql (Cloud SQL) 的 google app engine 是否支持 GIS?

javascript - 从html代码调用python函数

python - 无法使用 pip 安装 openpifpaf 和 pycocotools。错误: Failed building wheel for openpifpaf/pycocotools

java - 在 Google App Engine 上返回空列表在开发服务器上和部署时的行为有所不同