简体   繁体   English

如何在Mac OS X 10.6上使用virtualenv与Google App Engine SDK

[英]How to use virtualenv with Google App Engine SDK on Mac OS X 10.6

I am pulling my hair out trying to figure this out because I had it working until last week and somehow it broke. 我正在试着弄清楚我的头发,因为我让它工作到上周,不知何故它破了。

When I setup a virtualenv for a Google App Engine app and start the app with dev_appserver.py , I get errors importing the standard library (like "ImportError: No module named base64"). 当我为Google App Engine应用程序设置virtualenv并使用dev_appserver.py启动应用程序时,导致标准库导入错误(例如“ImportError:No module named base64”)。

Here's what I'm doing: 这是我正在做的事情:

(Using the system Python) (使用系统Python)

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

Then I add the a gae.pth file to ~/.virtualenv/foobar/lib/python2.5/site-packages/ containing the Google App Engine libraries: 然后我将一个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

(That's based on this answer .) (这是基于这个答案 。)

Then I source my "foobar" virtualenv and try to start my app with dev_appserver.py . 然后我采购我的“foobar”virtualenv并尝试使用dev_appserver.py启动我的应用程序。

The server starts but the first request errors out with the aforementioned "ImportError: No module named base64". 服务器启动但第一个请求错误,前面提到“ImportError:No module named base64”。 If I visit the admin console I get "ImportError: No module named cgi". 如果我访问管理控制台,我会得到“ImportError:没有名为cgi的模块”。

If I start up python, I can load these modules. 如果我启动python,我可以加载这些模块。

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

It seems that the SDK's sandboxing is preventing these libraries from getting loaded. SDK的沙盒似乎阻止了这些库的加载。 But like I said, I had this working until last week...something changed or I inadvertently broke my virtualenv and I can't figure out how I got it working in the first place. 但就像我说的那样,直到上周我一直在工作......有些事情发生了变化,或者我无意中打破了我的虚拟现实,我无法弄清楚我是如何让它在第一时间起作用的。

Software versions: 软件版本:

Google App Engine SDK 1.3.7 Google App Engine SDK 1.3.7
Mac OS X Snow Leopard 10.6.4 Mac OS X Snow Leopard 10.6.4
virtualenv 1.5.1 virtualenv 1.5.1

Update: In response to Alan Franzoni's questions: 更新:回应Alan Franzoni的问题:

I am using the system Python that came with Mac OS X. I installed virtualenv via easy_install. 我使用的是Mac OS X附带的Python系统。我通过easy_install安装了virtualenv。 I upgraded to virtualenv 1.5.1 today to try to fix the problem. 我今天升级到virtualenv 1.5.1以尝试解决问题。

If I run python /usr/local/bin/dev_appserver.py with the virtualenv python, the problem persists. 如果我使用virtualenv python运行python /usr/local/bin/dev_appserver.py ,则问题仍然存在。 If I deactivate the virtualenv and run that command with the system python2.5, it works. 如果我停用virtualenv并使用系统python2.5运行该命令,它可以工作。 (Also, I can use the GoogleAppEngineLauncher to start my app.) (另外,我可以使用GoogleAppEngineLauncher启动我的应用。)

Here is a full stack trace (this one uses the Kay framework, but the problem is the same with webapp): 这是一个完整的堆栈跟踪(这个使用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

It's an issue 4339 with the GAE SDK, it's confirmed and there are two slightly different patches available in the bug entry that make it work. 这是GAE SDK的问题4339 ,已经确认,并且bug条目中有两个稍微不同的补丁可以使它工作。

What happens is dev_appserver.py sets up a restricted python environment by disallowing access to any non-system-python modules and it does that by calculating the system python folder from the location of the os module. dev_appserver.py通过禁止访问任何非system-python模块来设置受限python环境,它通过从os模块的位置计算系统python文件夹来实现。 In a virtualenv instance the os.py gets symlinked into the virtualenv but gets compiled straight into virtualenv, and this is the path that dev_appserver uses, effectively blocking access to any module from the system python library that is not linked by virtualend, which is most of them. 在virtualenv实例中, os.pyos.py链接到virtualenv中,但直接编译成virtualenv,这是dev_appserver使用的路径,有效地阻止从未通过virtualend链接的系统python库访问任何模块,这是最多的他们 The solution is to "bless" both paths. 解决方案是“祝福”两条路径。

Google AppEngine SDK makes a lot of trick in order to pull its install into sys.path, and these tricks rely on actual file's path. 谷歌AppEngine SDK为了将其安装引入sys.path而制作了很多技巧,而这些技巧依赖于实际文件的路径。 I think there might be a lot of various reasons why it fails. 我认为可能有很多种原因导致它失败。 SDK doesn't install itself as a real python package, virtualenv doesn't do complete sandboxing, it just sets up an environment (obviously) and changes sys.path. SDK不会将自身安装为真正的python包,virtualenv不会完成沙盒,它只是设置一个环境(显然)并更改sys.path。 And GAE SDK does this too, they both intefere, SDK is being developing rapidly and changing often, so this is extremely bumpy road to go. 而且GAE SDK也是这样做的,他们都认为,SDK正在迅速发展和变化,所以这是非常坎坷的道路。

Probably, it would be better if you'd explain what are you trying to achieve. 也许,如果你解释你想要实现的目标会更好。 My guess is that you're trying to create a clean environment to ensure that no 3rd-party module is available to application. 我的猜测是,您正在尝试创建一个干净的环境,以确保没有第三方模块可用于应用程序。 If this guess is correct, I'd go with installing GAE SDK into virtualenv via requirements files as described here . 如果这个猜测是正确的,我会通过这里描述的需求文件将GAE SDK安装到virtualenv中。

I'm a little late to the conversation, but I was just having the same issue and I stumbled across gae_installer , which you can install in the usual way with pip install gae_installer . 我的谈话有点迟了,但我遇到了同样的问题,我偶然发现了gae_installer ,你可以通过pip install gae_installer以通常的方式pip install gae_installer This will put the google app engine (gae) sdk directly into your python path. 这会将谷歌应用引擎(gae)sdk直接放入你的python路径。 Hope others find this useful. 希望别人觉得这很有用。

I think, since you've setup virtualenv with the --no-site-packages option, you need to install the SDK into the environment. 我想,既然您已经使用--no-site-packages选项设置了virtualenv,则需要将SDK安装到环境中。 --no-site-packages seperates the dev environment you're configuring from any other Python installation on your computer so, as you seem to have it configured you're calling a module that doesn't exist (in the environment) which is why it works with the env deactivated (which is then running Python from the default OS installation). --no-site-packages从您计算机上的任何其他Python安装中分离您正在配置的开发环境,因此,您似乎已将其配置为调用不存在的模块(在环境中),这是为什么它与env停用(然后从默认的OS安装运行Python)。 Try setting up the dev env without the --no-site-packages option if you want to be able to access modules outside of the env. 如果您希望能够访问env之外的模块,请尝试在不使用--no-site-packages选项的情况下设置dev env。

Same answer as bozzo. 与博佐相同的答案。 Here's instructions: 这是说明:

This is described in Issue 4339 for GAE. 这在GAE的问题4339中有所描述。 Here's how to fix it: 以下是修复方法:

  1. Download patch here: patch 在这里下载补丁: 补丁
  2. Move the patch to google_appengine/google/appengine/tools/ 将补丁移至google_appengine / google / appengine / tools /
  3. Change your working directory to the same path as above 将您的工作目录更改为与上面相同的路径
  4. Type: patch -p0 < dev_appserver.patch 键入: patch -p0 <dev_appserver.patch

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM