无法用nosegae导入django包

时间:2011-09-16 02:28:12

标签: python google-app-engine nose

我正在尝试开始使用nosegae,但是我遇到了一个问题,即使是使用django时最简单的情况,我似乎无法通过它。

在没有--without-sandbox标志的情况下运行时,以下测试都会失败

def test_import_django  ():
    import django

def test_import_django_http  ():
    import django.http

Traceback (most recent call last):
  File "C:\Python27\lib\site-packages\nose-1.1.2-py2.7.egg\nose\case.py", line 1
97, in runTest
    self.test(*self.arg)
  File "C:\Users\User\Desktop\TDD_GAE\myproj\tests.py", line 2, in test_import_d
jango
    import django
  File "C:\Python27\lib\site-packages\nosegae-0.1.9-py2.7.egg\nosegae.py", line
207, in find_module
    return super(HookMixin, self).find_module(fullname, path)
  File "C:\Program Files (x86)\Google\google_appengine\google\appengine\tools\de
v_appserver.py", line 1505, in Decorate
    return func(self, *args, **kwargs)
  File "C:\Program Files (x86)\Google\google_appengine\google\appengine\tools\de
v_appserver.py", line 1998, in find_module
    search_path)
  File "C:\Program Files (x86)\Google\google_appengine\google\appengine\tools\de
v_appserver.py", line 1505, in Decorate
    return func(self, *args, **kwargs)
  File "C:\Program Files (x86)\Google\google_appengine\google\appengine\tools\de
v_appserver.py", line 2119, in FindModuleRestricted
    result = self.FindPathHook(submodule, submodule_fullname, path_entry)
  File "C:\Program Files (x86)\Google\google_appengine\google\appengine\tools\de
v_appserver.py", line 2219, in FindPathHook
    return self._imp.find_module(submodule, [path_entry])

如果我至少使用--without-sandbox第一次测试通过

,那该怎么办?
myproj.tests.test_import_django ... ok
myproj.tests.test_import_django_http ... ERROR

======================================================================
ERROR: myproj.tests.test_import_django_http
----------------------------------------------------------------------
Traceback (most recent call last):
  File "C:\Python27\lib\site-packages\nose-1.1.2-py2.7.egg\nose\case.py", line 1
97, in runTest
    self.test(*self.arg)
  File "C:\Users\User\Desktop\TDD_GAE\myproj\tests.py", line 5, in test_import_d
jango_http
    import django.http
  File "C:\Program Files (x86)\Google\google_appengine\lib\django_1_2\django\htt
p\__init__.py", line 9, in <module>
    from mod_python.util import parse_qsl
  File "C:\Python27\lib\site-packages\nosegae-0.1.9-py2.7.egg\nosegae.py", line
199, in find_module
    mod_path = self.find_mod_path(fullname)
  File "C:\Python27\lib\site-packages\nosegae-0.1.9-py2.7.egg\nosegae.py", line
251, in find_mod_path
    _sf, path, _desc= self._imp.find_module(top, None)
AttributeError: 'str' object has no attribute 'find_module'

有没有人遇到并知道如何解决这个问题?

修改

似乎问题是递归导入

def test_import_pdb ():
    import pdb
    pdb.set_trace ()

堆栈跟踪的一部分是

File "C:\Python27\lib\pdb.py", line 72, in __init__
  import readline

请注意import __init__中的django.http也是堆栈跟踪的一部分

1 个答案:

答案 0 :(得分:1)

阅读有关Django测试的https://docs.djangoproject.com/en/dev/topics/testing/

我知道最好使用随django一起提供的unittest或doctest,因为它对django特定的测试有一些改进,比如表单字段输出测试和一些数据库功能。 Hovewer它不是必需的,如果你想继续使用鼻子 - 认为你错过了django环境设置:

from django.test.utils import setup_test_environment
setup_test_environment()

此行需要在./manage.py --test

之外运行测试

<强> UPD 是的,我以前的想法是错的。所以我只是深入研究了鼻子和鼻子的来源,以及我的想法 - 检查你的鼻子版本中的HardenedModulesHook定义,因为我发现了以下鼻子:

class HardenedModulesHook(object):
    ...
    def __init__(self,
               module_dict,
               imp_module=imp,
               os_module=os,
               dummy_thread_module=dummy_thread,
               pickle_module=pickle):
    ...

这给出了以下内容 - 当执行noseGAE插件begin()方法时 - &gt;调用self._install_hook(dev_appserver.HardenedModulesHook)来声明mix-hook类并创建它的实例,如self.hook = Hook(sys.modules, self._path)。 &lt; - HardenedModulesHook.__init__调用第二个参数为mystic'_path'但是在NOSE中这个参数默认为'imp'模块 - &gt;这让你有一个例外:

    _sf, path, _desc= self._imp.find_module(top, None)
AttributeError: 'str' object has no attribute 'find_module'  

所以我认为这可能是一个问题:[/ p>