本文介绍了“测试模块"导入不正确的原因是什么?吝啬的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我逐行复制了一个有效的测试,只是更改了一些名称(至少我如此认为),现在我得到了一个非常神秘的错误:(我用FOO,BAR替换了一些东西)

I have copied a working test line by line and just changed a few names (at least so I thought) and now I get this very cryptic error: (I have replaced some stuff with FOO, BAR)

ImportError: 'tests' module incorrectly imported from 'FOO/exports/tests'. Expected 'FOO/exports'. Is this module globally installed?

问题是我根本不理解该错误.此错误消息是什么意思?

The problem is that I do not understand the error at all. What does this error message mean?

完整的堆栈跟踪:

Traceback (most recent call last):
  File "BAR/modeling/manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/Users/jonathan/anaconda/lib/python2.7/site-packages/django/core/management/__init__.py", line 353, in execute_from_command_line
    utility.execute()
  File "/Users/jonathan/anaconda/lib/python2.7/site-packages/django/core/management/__init__.py", line 345, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/Users/jonathan/anaconda/lib/python2.7/site-packages/django/core/management/commands/test.py", line 30, in run_from_argv
    super(Command, self).run_from_argv(argv)
  File "/Users/jonathan/anaconda/lib/python2.7/site-packages/django/core/management/base.py", line 348, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/Users/jonathan/anaconda/lib/python2.7/site-packages/django/core/management/commands/test.py", line 74, in execute
    super(Command, self).execute(*args, **options)
  File "/Users/jonathan/anaconda/lib/python2.7/site-packages/django/core/management/base.py", line 399, in execute
    output = self.handle(*args, **options)
  File "/Users/jonathan/anaconda/lib/python2.7/site-packages/django/core/management/commands/test.py", line 90, in handle
    failures = test_runner.run_tests(test_labels)
  File "/Users/jonathan/anaconda/lib/python2.7/site-packages/django/test/runner.py", line 531, in run_tests
    suite = self.build_suite(test_labels, extra_tests)
  File "/Users/jonathan/anaconda/lib/python2.7/site-packages/django/test/runner.py", line 451, in build_suite
    tests = self.test_loader.discover(start_dir=label, **kwargs)
  File "/Users/jonathan/anaconda/lib/python2.7/unittest/loader.py", line 206, in discover
    tests = list(self._find_tests(start_dir, pattern))
  File "/Users/jonathan/anaconda/lib/python2.7/unittest/loader.py", line 267, in _find_tests
    raise ImportError(msg % (mod_name, module_dir, expected_dir))
ImportError: 'tests' module incorrectly imported from 'FOO/exports/tests'. Expected 'FOO/exports'. Is this module globally installed?

推荐答案

根据我的经验,运行测试时出现奇怪的ImportErrors是由tests模块本身中的ImportError引起的.

In my experience, weird ImportErrors when running tests are caused by an ImportError in the tests module itself.

确保可以导入测试模块:

Ensure that your tests module can be imported:

$ python manage.py shell
...
>>> import foo.exports.tests

如果这导致错误,请确保您没有目录 foo/exports/tests 和文件 foo/exports/tests.py

If that causes an error, make sure you do not have both a directory foo/exports/tests and a file foo/exports/tests.py

这篇关于“测试模块"导入不正确的原因是什么?吝啬的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-11 18:31