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

发布于 2024-09-26 01:34:08 字数 5320 浏览 6 评论 0原文

我正在费力地想弄清楚这个问题,因为直到上周我才让它工作,但不知何故它坏了。

当我为 Google App Engine 应用程序设置 virtualenv 并使用 dev_appserver.py 启动该应用程序时,我在导入标准库时遇到错误(例如“ImportError:没有名为 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

基于 这个答案。)

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

服务器启动,但第一个请求出错,并显示上述“ImportError:没有名为 base64 的模块”。如果我访问管理控制台,我会收到“ImportError:没有名为 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
virtualenv 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

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").

Here's what I'm doing:

(Using the system 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:

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

The server starts but the first request errors out with the aforementioned "ImportError: No module named base64". If I visit the admin console I get "ImportError: No module named cgi".

If I start up python, I can load these modules.

>>> 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. 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
Mac OS X Snow Leopard 10.6.4
virtualenv 1.5.1

Update: In response to Alan Franzoni's questions:

I am using the system Python that came with Mac OS X. I installed virtualenv via easy_install. I upgraded to virtualenv 1.5.1 today to try to fix the problem.

If I run python /usr/local/bin/dev_appserver.py with the virtualenv python, the problem persists. If I deactivate the virtualenv and run that command with the system python2.5, it works. (Also, I can use the GoogleAppEngineLauncher to start my app.)

Here is a full stack trace (this one uses the Kay framework, but the problem is the same with 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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

双手揣兜 2024-10-03 01:34:08

这是 GAE SDK 的问题 4339,已确认且存在错误条目中提供了两个略有不同的补丁,使其能够正常工作。

发生的情况是 dev_appserver.py 通过禁止访问任何非系统 python 模块来设置受限制的 python 环境,并通过从 os< 的位置计算系统 python 文件夹来实现这一点。 /代码> 模块。在 virtualenv 实例中,os.py 符号链接到 virtualenv,但直接编译到 virtualenv,这是 dev_appserver 使用的路径,有效阻止从以下位置访问任何模块没有通过virtualend链接的系统python库,这是其中的大部分。解决办法是“祝福”这两条路。

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.

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. 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. The solution is to "bless" both paths.

死开点丶别碍眼 2024-10-03 01:34:08

Google AppEngine SDK 使用了很多技巧来将其安装拉入 sys.path,而这些技巧依赖于实际文件的路径。我认为失败的原因可能有很多。 SDK 不会将自身安装为真正的 python 包,virtualenv 不会执行完整的沙箱,它只是设置一个环境(显然)并更改 sys.path。而GAE SDK也是这样做的,两者是相互干扰的,SDK正在快速发展且经常变化,所以这是一条极其坎坷的路。

如果您能解释一下您想要实现的目标,可能会更好。我的猜测是,您正在尝试创建一个干净的环境,以确保没有第三方模块可供应用程序使用。如果这个猜测是正确的,我会按照此处<所述的要求文件将 GAE SDK 安装到 virtualenv 中< /a>.

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

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.

度的依靠╰つ 2024-10-03 01:34:08

我认为,既然您已经使用 --no-site-packages 选项设置了 virtualenv,那么您需要将 SDK 安装到环境中。 --no-site-packages 将您正在配置的开发环境与计算机上的任何其他 Python 安装分开,因此,当您似乎已配置它时,您正在调用一个不存在(在环境中)的模块,该模块是为什么它可以在 env 停用的情况下工作(然后从默认操作系统安装运行 Python)。如果您希望能够访问环境之外的模块,请尝试在不使用 --no-site-packages 选项的情况下设置开发环境。

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

赏烟花じ飞满天 2024-10-03 01:34:08

我的谈话有点晚了,但我遇到了同样的问题,我偶然发现了 gae_installer ,您可以使用 pip install gae_installer 以常规方式安装。这会将 google app engine (gae) sdk 直接放入您的 python 路径中。希望其他人觉得这很有用。

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. This will put the google app engine (gae) sdk directly into your python path. Hope others find this useful.

往日 2024-10-03 01:34:08

与博佐相同的答案。说明如下:

问题 4339 对于 GAE。修复方法如下:

  1. 在此处下载补丁:patch
  2. 将补丁移至 google_appengine/google/appengine/tools/
  3. 将工作目录更改为与上述相同的路径
  4. 键入:patch - p0< dev_appserver.patch

Same answer as bozzo. Here's instructions:

This is described in Issue 4339 for GAE. Here's how to fix it:

  1. Download patch here: patch
  2. Move the patch to google_appengine/google/appengine/tools/
  3. Change your working directory to the same path as above
  4. Type: patch -p0 < dev_appserver.patch
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文