问题描述
我试图弄清楚这一点,因为我一直在努力,直到上周,不知何故,它破灭了。当我为Google App Engine应用程序设置virtualenv并使用 dev_appserver.py
启动应用程序时,我得到导入标准库的错误(例如ImportError:No module named base64)。
以下是我正在做的事:
(使用系统Python)
virtualenv --python = python2.5 --no-site-packages〜/ .virtualenv / foobar
然后我将 gae.pth
文件添加到<$ c包含Google App Engine库的$ c>〜/ .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
通过。)
然后我输入我的foobarvirtualenv并尝试用 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,我无法弄清楚我是如何在第一时间完成工作的。
$ b
Google App Engine SDK 1.3.7
Mac OS X Snow Leopard 10.6.4
virtualenv 1.5.1
更新:回应Alan Franzoni的问题:
我使用的是Mac OS X附带的系统Python。我通过easy_install安装了virtualenv。如果我运行 python /usr/local/bin/dev_appserver.py
与virtualenv python,问题依然存在。如果我停用virtualenv并使用系统python2.5运行该命令,它将起作用。 (另外,我可以使用GoogleAppEngineLauncher启动我的应用程序。)
这是一个完整的堆栈跟踪(这个使用Kay框架,但问题与webapp):
pre code $ Traceback(最近一次调用最后一次)
文件/Applications/GoogleAppEngineLauncher.app/Contents/资源/ GoogleAppEngine-default.bundle / Contents / Resources / google_appengine / google / appengine / tools / dev_appserver.py,行3206,在_HandleRequest
self._Dispatch(dispatcher,self.rfile,outfile,env_dict)
文件/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/tools/dev_appserver.py,第3149行,位于_Dispatch
base_env_dict = env_dict )
文件/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/tools/dev_appserver.py,第525行,发送
base_env_dict = base_en v_dict)
文件/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/tools/dev_appserver.py,行2402,在Dispatch $ b $中b self._module_dict)
在ExecuteCGI中的文件/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/tools/dev_appserver.py,第2312行
reset_modules = exec_script(handler_path,cgi_path,hook)
文件/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/tools/dev_appserver .py,第2208行,在ExecuteOrImportScript中
exec module_code in script_module .__ dict__
在< module>文件中的/Users/look/myapp/kay/main.py行17,
kay.setup()
文件/Users/look/myapp/kay/__init__.py,第122行,设置
from google.appengine.ext import db
文件/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/tools/dev_appserver.py,第1287行,装饰
return func(self, * args,** kwargs)
文件/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/tools/dev_appserver.py,第1937行, in load_module
return self.FindAndLoadModule(submodule,fullname,search_path)
文件/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/ tools / dev_appserver.py,第1287行,装饰
返回func(self,* args,** kwargs)
文件/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppE在FindAndLoadModule
描述)
文件中的ngine-default.bundle / Contents / Resources / google_appengine / google / appengine / tools / dev_appserver.py,第1839行)/Applications/GoogleAppEngineLauncher.app/Contents/Resources /GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/tools/dev_appserver.py,第1287行,装饰
返回func(self,* args,** kwargs)
文件/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/tools/dev_appserver.py,第1790行,位于LoadModuleRestricted
description)
文件/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/ext/db/__init__.py,第81行,位于< module>
import base64
ImportError:没有名为base64的模块
这是GAE SDK的 ,它已被证实,并且在错误条目中有两个稍微不同的修补程序可用。
发生什么是 dev_appserver.py
通过禁止访问任何非系统python来设置受限制的python环境模块,它通过从 os
模块的位置计算系统python文件夹来实现。在一个virtualenv实例中, os.py
被symlinked到virtualenv中,但是直接被编译成virtualenv,这是dev_appserver使用的路径,阻止访问未通过virtualend链接的系统python库中的任何模块,这是其中的大多数。解决方法是祝福两条路径。
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
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.
这篇关于如何在Mac OS X 10.6上使用带有Google App Engine SDK的virtualenv的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!