django.test.client 上的 Django rest 框架导入错误

Posted

技术标签:

【中文标题】django.test.client 上的 Django rest 框架导入错误【英文标题】:Django rest framework import error in on django.test.client 【发布时间】:2017-07-17 22:17:40 【问题描述】:

我正在使用谷歌应用引擎开发服务器来运行在 mac os 上的 python。 我正在运行的项目是一个使用 djangae 和 django rest 框架的 appengine django 项目。

项目中的一切工作正常,但是一旦声明与 rest_framework 相关的导入,即使我目前没有运行任何测试,我也会收到关于 django.test.client 的错误。

我尝试执行的导入是“from rest_framework.decoraters import api_view”,只要我进行此导入或与其余框架相关的任何其他导入。

这是我在每次导入涉及其余框架时遇到的错误。

ERROR    2017-02-27 10:19:08,019 base.py:256] Internal Server Error: /_ah/warmup
Traceback (most recent call last):
File "/Users/MyUser/git/project-name/src/lib/django/core/handlers/base.py", line 223, in get_response
    response = middleware_method(request, response)
File "/Users/MyUser/git/project-name/src/core/middleware/url_definition.py", line 32, in process_response
    response = redirect('%s/' % request.path)
File "/Users/MyUser/git/project-name/src/lib/django/shortcuts.py", line 116, in redirect
    return redirect_class(resolve_url(to, *args, **kwargs))
File "/Users/MyUser/git/project-name/src/lib/django/shortcuts.py", line 205, in resolve_url
    return urlresolvers.reverse(to, args=args, kwargs=kwargs)
File "/Users/MyUser/git/project-name/src/lib/django/core/urlresolvers.py", line 578, in reverse
    return force_text(iri_to_uri(resolver._reverse_with_prefix(view, prefix, *args, **kwargs)))
File "/Users/MyUser/git/project-name/src/lib/django/core/urlresolvers.py", line 432, in _reverse_with_prefix
    self._populate()
File "/Users/MyUser/git/project-name/src/lib/django/core/urlresolvers.py", line 284, in _populate
    for pattern in reversed(self.url_patterns):
File "/Users/MyUser/git/project-name/src/lib/django/core/urlresolvers.py", line 401, in url_patterns
    patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
File "/Users/MyUser/git/project-name/src/lib/django/core/urlresolvers.py", line 395, in urlconf_module
    self._urlconf_module = import_module(self.urlconf_name)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/importlib/__init__.py", line 37, in import_module
    __import__(name)
File "/Users/MyUser/git/project-name/src/core/urls.py", line 34, in <module>
    url(r'^', include('pages.urls'), name='pages.urls')
File "/Users/MyUser/git/project-name/src/lib/django/conf/urls/__init__.py", line 33, in include
    urlconf_module = import_module(urlconf_module)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/importlib/__init__.py", line 37, in import_module
    __import__(name)
File "/Users/MyUser/git/project-name/src/pages/urls.py", line 31, in <module>
    url(r'^myapp/', include('pages.myapp.urls', namespace='rest_framework'), name="pages.myapp"),
File "/Users/MyUser/git/project-name/src/lib/django/conf/urls/__init__.py", line 33, in include
    urlconf_module = import_module(urlconf_module)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/importlib/__init__.py", line 37, in import_module
    __import__(name)
File "/Users/MyUser/git/project-name/src/pages/myapp/urls.py", line 4, in <module>
    url(r'^finance/', include('pages.myapp.finance.urls'), name='pages.myapp.finance'),
File "/Users/MyUser/git/project-name/src/lib/django/conf/urls/__init__.py", line 33, in include
    urlconf_module = import_module(urlconf_module)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/importlib/__init__.py", line 37, in import_module
    __import__(name)
File "/Users/MyUser/git/project-name/src/pages/myapp/finance/urls.py", line 4, in <module>
    url(r'^api/', include('pages.myapp.finance.api.urls'), name='pages.myapp.finance.api.urls'),
File "/Users/MyUser/git/project-name/src/lib/django/conf/urls/__init__.py", line 33, in include
    urlconf_module = import_module(urlconf_module)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/importlib/__init__.py", line 37, in import_module
    __import__(name)
File "/Users/MyUser/git/project-name/src/pages/myapp/finance/api/urls.py", line 4, in <module>
    url(r'^service/', include('pages.myapp.finance.api.service.urls'), name='pages.myapp.finance.api.service'),
File "/Users/MyUser/git/project-name/src/lib/django/conf/urls/__init__.py", line 33, in include
    urlconf_module = import_module(urlconf_module)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/importlib/__init__.py", line 37, in import_module
    __import__(name)
File "/Users/MyUser/git/project-name/src/pages/myapp/finance/api/service/urls.py", line 8, in <module>
    url(r'^user/', include('pages.myapp.finance.api.service.user.urls'),
File "/Users/MyUser/git/project-name/src/lib/django/conf/urls/__init__.py", line 33, in include
    urlconf_module = import_module(urlconf_module)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/importlib/__init__.py", line 37, in import_module
    __import__(name)
File "/Users/MyUser/git/project-name/src/pages/myapp/finance/api/service/user/urls.py", line 3, in <module>
    from pages.myapp.finance.api.service.user import functions
File "/Users/MyUser/git/project-name/src/pages/myapp/finance/api/service/user/functions.py", line 5, in <module>
    from rest_framework.decorators import api_view
File "/Users/MyUser/git/project-name/src/lib/rest_framework/decorators.py", line 15, in <module>
    from rest_framework.views import APIView
File "/Users/MyUser/git/project-name/src/lib/rest_framework/views.py", line 19, in <module>
    from rest_framework.request import Request
File "/Users/MyUser/git/project-name/src/lib/rest_framework/request.py", line 23, in <module>
    from rest_framework.settings import api_settings
File "/Users/MyUser/git/project-name/src/lib/rest_framework/settings.py", line 24, in <module>
    from django.test.signals import setting_changed
File "/Users/MyUser/git/project-name/src/lib/django/test/__init__.py", line 5, in <module>
File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/tools/devappserver2/python/sandbox.py", line 999, in load_module
    raise ImportError('No module named %s' % fullname)
ImportError: No module named django.test.client

【问题讨论】:

你是如何包含这些库的?似乎您使用的 django 版本没有 DRF 所需的 django.test.client:github.com/tomchristie/django-rest-framework/blob/… @chachan 我使用基本修复路径设置将 django 添加到项目 init 上的 python 路径中,我使用的 django 版本是 Django 1.8.2,并且确实包含 django.test.client 但不管它说没有。 【参考方案1】:

如果您在应用引擎上遇到此类导入错误,请务必检查您在 app.yaml 中的 skip_files

例如:

skip_files:
  - ^(.*/)?.*/test/.*$

使“django/test”文件夹无法在您的开发环境中导入,并且该文件夹不会部署到应用引擎。

【讨论】:

以上是关于django.test.client 上的 Django rest 框架导入错误的主要内容,如果未能解决你的问题,请参考以下文章

6.Django基于模拟浏览器的测试用例编写python-slugifydjango-taggit

centos 6.5 上 Apache 2.2.15 上的 Django

{Django基础八之cookie和session}一 会话跟踪 二 cookie 三 django中操作cookie 四 session 五 dja

浏览器上的错误 404 未显示静态文件夹文件

什么是java中的war文件备用?

django.db.utils.ProgrammingError: 关系已经存在于 OenBSD vps