无法使用 nosegae 导入 django 包

发布于 2024-12-05 06:33:33 字数 3105 浏览 1 评论 0原文

我正在尝试开始使用 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

注意到django.http<的__init__中的导入 /code> 也是堆栈跟踪的一部分

I am trying to get started with using nosegae, however I run into the issue that I can't seem to get it to pass even the simplest of cases when using django.

when running without the --without-sandbox flag both the following tests fail

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

Howevere if I do use --without-sandbox at least the first test passes

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'

Has anyone encountered and know how I can go about past this?

Edit

It seems that the issue is recursive imports

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

part of the stack trace is

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

notice that an import in __init__ of django.http is also part of the stack trace

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

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

发布评论

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

评论(1

枫以 2024-12-12 06:33:33

阅读 https://docs.djangoproject.com/en/dev/topics/testing/< /a> 关于 Django 测试。

据我所知,最好使用 django 附带的 unittest 或 doctest,因为它对 django 特定的测试有一些改进,例如表单字段输出测试和一些数据库功能。然而,这不是必需的,如果您想继续使用鼻子 - 认为您错过了 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):
    ...

这给出了以下内容 - 当 nosGAE 插件 begin()方法被执行->调用 self._install_hook(dev_appserver.HardenedModulesHook) 来声明混合钩子类并创建它的实例,如 self.hook = Hook(sys.modules, self._path) 。 <- 有 HardenedModulesHook.__init__ 被调用,第二个参数为神秘的“_path”,但是在 NOSE 中,默认情况下这个参数应该是“imp”模块 ->这使得你有一个例外:

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

所以我认为这可能是鼻子盖的问题:(

Read https://docs.djangoproject.com/en/dev/topics/testing/ about Django testing.

As I know it's better to use unittest or doctest shipped with django as it have several improvements for django-specific testing like form field output testing and some database features. Hovewer it's not essential and if you want to continue using nose - think you missed django environment setup:

from django.test.utils import setup_test_environment
setup_test_environment()

This lines needed to run your tests outside of ./manage.py --test

UPD
Yeah my previous thought's were wrong. So I just digged into sources of nose and nose-gae, and what I think - check HardenedModulesHook definition in your nose version, cause in trunk of nose I've found following:

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

That gives following - when noseGAE plugin begin() method is executed -> there self._install_hook(dev_appserver.HardenedModulesHook) is called which declares mixed-hook class and creates it's instance like self.hook = Hook(sys.modules, self._path). <- There is HardenedModulesHook.__init__ called with second argument as mystic '_path' however in NOSE this argument should be 'imp' module by default -> That makes an exception you've got:

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

So I think it might be a problem with nose-gae :(

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文