我已经逐行复制了一个工作测试并且只更改了一些名称(至少我认为如此)现在我得到了这个非常神秘的错误:(我用FOO,BAR替换了一些东西)
ImportError: 'tests' module incorrectly imported from 'FOO/exports/tests'. Expected 'FOO/exports'. Is this module globally installed?
Run Code Online (Sandbox Code Playgroud)
问题是我根本不理解错误.这个错误信息是什么意思?
完成堆栈跟踪:
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?
Run Code Online (Sandbox Code Playgroud)
Dan*_*per 143
根据我的经验,运行测试时奇怪的ImportErrors是由测试模块本身的ImportError引起的.
确保可以导入测试模块:
$ python manage.py shell
...
>>> import foo.exports.tests
Run Code Online (Sandbox Code Playgroud)
编辑:
如果这导致错误,请确保您没有目录foo/exports/tests和文件foo/exports/tests.py
Bre*_*inn 40
正如Daniel Hepper在上面的评论中所说,请尝试检查您的应用中是否同时包含app/tests文件夹和app/tests.py文件.
Django 自动startapp创建一个tests.py文件,因此可能有一个你没有注意到的文件.
如果您只是删除自动生成的tests.py文件,它应该工作.(显然你应该在删除之前检查文件的内容!)
小智 12
如果你已经创建了一个名为tests和已经编写测试文件里面,例如test_views.py,test_models.py等等,一定要删除该文件test.py由命令自动创建python manage.py startapp。
一言以蔽之:删除 test.py 或测试文件夹 当我将之前在一个项目中编写的一些测试复制到我的新项目中时,我遇到了同样的问题,其中我有超过 5 个相同的 API。通常我会创建一个名为测试的新文件夹,并将所有测试写入每个应用程序的一个文件夹中,这样一切看起来都会更好我犯的导致我解决此问题的错误是在创建测试文件夹时没有从应用程序文件夹中删除 test.py 文件在同一个应用程序中因为你不能在同一个应用程序中同时拥有测试文件夹和 test.py 。
尝试检查您是否同时拥有 app/tests文件夹和 app/tests.py
文件在您的应用程序中。
默认情况下,会自动调用一个文件,tests.py删除此文件,错误将得到解决
| 归档时间: |
|
| 查看次数: |
11835 次 |
| 最近记录: |