GAEUnit-ImportError: Access to module file denied
最近学习GAE,单元测试少不了的,写了一大段代码,结果测试一片红,错误提示信息:
GAEUnit错误堆栈信息
Traceback (most recent call last):
File "D:\Program Files\Google\google_appengine\google\appengine\ext\webapp\__init__.py", line 507, in __call__
handler.get(*groups)
File "E:\__webprj__\cookresume\gaeunit.py", line 230, in get
_load_default_test_modules()
File "E:\__webprj__\cookresume\gaeunit.py", line 291, in _load_default_test_modules
return [reload(__import__(name)) for name in module_names]
File "D:\Program Files\Google\google_appengine\google\appengine\tools\dev_appserver.py", line 1163, in Decorate
return func(self, *args, **kwargs)
File "D:\Program Files\Google\google_appengine\google\appengine\tools\dev_appserver.py", line 1469, in find_module
search_path)
File "D:\Program Files\Google\google_appengine\google\appengine\tools\dev_appserver.py", line 1163, in Decorate
return func(self, *args, **kwargs)
File "D:\Program Files\Google\google_appengine\google\appengine\tools\dev_appserver.py", line 1569, in FindModuleRestricted
raise ImportError(error_message)
ImportError: Access to module file denied: D:\Python25\lib\site-packages\PIL\__init__.py
File "D:\Program Files\Google\google_appengine\google\appengine\ext\webapp\__init__.py", line 507, in __call__
handler.get(*groups)
File "E:\__webprj__\cookresume\gaeunit.py", line 230, in get
_load_default_test_modules()
File "E:\__webprj__\cookresume\gaeunit.py", line 291, in _load_default_test_modules
return [reload(__import__(name)) for name in module_names]
File "D:\Program Files\Google\google_appengine\google\appengine\tools\dev_appserver.py", line 1163, in Decorate
return func(self, *args, **kwargs)
File "D:\Program Files\Google\google_appengine\google\appengine\tools\dev_appserver.py", line 1469, in find_module
search_path)
File "D:\Program Files\Google\google_appengine\google\appengine\tools\dev_appserver.py", line 1163, in Decorate
return func(self, *args, **kwargs)
File "D:\Program Files\Google\google_appengine\google\appengine\tools\dev_appserver.py", line 1569, in FindModuleRestricted
raise ImportError(error_message)
ImportError: Access to module file denied: D:\Python25\lib\site-packages\PIL\__init__.py
毛老爷说发现问题分析问题解决问题,可我怎么分析来分析去也不明白为什么要import PIL。一段段咬代码,幸好gaeunit.py很短很强悍,不过还是没看出哪根筋不对。幸好毛老爷还说实践是解决真理的唯一标准,所以就把
_load_default_test_modules的几行代码扒拉出来执行一遍。发现一个小线索:
- reload(__import(name) for name in module_names)有一个module是__init__
- sys.path中有一个D:\Python25\lib\site-packages\PIL
这也算想明白了,为什么GAEUnit至死不渝的要import PIL,就因为这__init__.py和sys.path的顺序引起的,python就是通过这么个简单的途径来区分是package还是文件夹,当然还有其他用途就不细说了。偶的习惯会加个__init__.py,结果就错了。
解决办法:
修改_load_default_test_modules
排除__init__1 module_names = [mf[0:-3] for mf in os.listdir(_DEFAULT_TEST_DIR) if mf.endswith(".py") and not mf.startswith("__init__")]