ImportError:导入测试模块失败

时间:2014-02-02 16:09:19

标签: python django python-2.7

尝试运行一些测试,我遇到了这个错误,即使用Python 2.7.3和Django 1.5。

python -m unittest discover
E
======================================================================
ERROR: mapvis.test_select_nodes_from_rectangle (unittest.loader.ModuleImportFailure)
----------------------------------------------------------------------
ImportError: Failed to import test module: mapvis.test_select_nodes_from_rectangle
Traceback (most recent call last):
  File "/usr/lib/python2.7/unittest/loader.py", line 252, in _find_tests
    module = self._get_module_from_name(name)
  File "/usr/lib/python2.7/unittest/loader.py", line 230, in _get_module_from_name
    __import__(name)
  File "/home/pierre/maps3/lmap/mapvis/test_select_nodes_from_rectangle.py", line 5, in <module>
    from django.core.context_processors import csrf
  File "/usr/local/lib/python2.7/dist-packages/django/core/context_processors.py", line 12, in <module>
    from django.middleware.csrf import get_token
  File "/usr/local/lib/python2.7/dist-packages/django/middleware/csrf.py", line 16, in <module>
    from django.utils.cache import patch_vary_headers
  File "/usr/local/lib/python2.7/dist-packages/django/utils/cache.py", line 26, in <module>
    from django.core.cache import get_cache
  File "/usr/local/lib/python2.7/dist-packages/django/core/cache/__init__.py", line 70, in <module>
    if DEFAULT_CACHE_ALIAS not in settings.CACHES:
  File "/usr/local/lib/python2.7/dist-packages/django/conf/__init__.py", line 52, in __getattr__
    self._setup(name)
  File "/usr/local/lib/python2.7/dist-packages/django/conf/__init__.py", line 45, in _setup
    % (desc, ENVIRONMENT_VARIABLE))
ImproperlyConfigured: Requested setting CACHES, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.

test_select_nodes_from_rectangle.py

from django.core.context_processors import csrf
....
....
import random
import unittest

....
....
....

if __name__ == '__main__':
    unittest.main()

1 个答案:

答案 0 :(得分:1)

根据错误消息中的建议设置环境变量,让Django知道哪个文件包含项目设置:

DJANGO_SETTINGS_MODULE=settings.module python -m unittest discover

或者启动这样的测试:

./manage.py test