Python AttributeError: 'module' 对象没有属性 'tests'
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25575073/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
AttributeError: 'module' object has no attribute 'tests'
提问by Chris
I'm running this command:
我正在运行这个命令:
python manage.py test project.apps.app1.tests
and it causes this error:
并导致此错误:
AttributeError: 'module' object has no attribute 'tests'
AttributeError: 'module' 对象没有属性 'tests'
Below is my directory structure. I've also added app1 to my installed apps config.
下面是我的目录结构。我还将 app1 添加到我安装的应用程序配置中。
Traceback (most recent call last):
File "manage.py", line 10, in <module> execute_from_command_line(sys.argv)
File "/home/username/local/dev/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 385, in execute_from_command_line
utility.execute()
File "/home/username/local/dev/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 377, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/home/username/local/dev/local/lib/python2.7/site-packages/django/core/management/commands/test.py", line 50, in run_from_argv
super(Command, self).run_from_argv(argv)
File "/home/username/local/dev/local/lib/python2.7/site-packages/django/core/management/base.py", line 288, in run_from_argv
self.execute(*args, **options.__dict__)
File "/home/username/local/dev/local/lib/python2.7/site-packages/django/core/management/commands/test.py", line 71, in execute
super(Command, self).execute(*args, **options)
File "/home/username/local/dev/local/lib/python2.7/site-packages/django/core/management/base.py", line 338, in execute
output = self.handle(*args, **options)
File "/home/username/local/dev/local/lib/python2.7/site-packages/django/core/management/commands/test.py", line 88, in handle
failures = test_runner.run_tests(test_labels)
File "/home/username/local/dev/local/lib/python2.7/site-packages/django/test/runner.py", line 146, in run_tests
suite = self.build_suite(test_labels, extra_tests)
File "/home/username/local/dev/local/lib/python2.7/site-packages/django/test/runner.py", line 66, in build_suite
tests = self.test_loader.loadTestsFromName(label)
File "/usr/lib/python2.7/unittest/loader.py", line 100, in loadTestsFromName
parent, obj = obj, getattr(obj, part)
AttributeError: 'module' object has no attribute 'tests'
Directory structure:
目录结构:


采纳答案by Chris
I finally figured it out working on another problem. The problem was that my test couldn't find an import.
我终于想通了解决另一个问题。问题是我的测试找不到导入。
It looks like you get the above error if your test fails to import. This makes sense because the test suite can't import a broken test. At least I think this is what is going on because I fixed the import within my test file and sure enough it started working.
如果您的测试无法导入,您似乎会收到上述错误。这是有道理的,因为测试套件无法导入损坏的测试。至少我认为这是正在发生的事情,因为我在我的测试文件中修复了导入,并且确实它开始工作了。
To validate your test case just try import the test case file in python console.
要验证您的测试用例,只需尝试在 python 控制台中导入测试用例文件。
Example:
例子:
from project.apps.app1.tests import *
回答by Kasramvd
According to django document When you run your tests, the default behavior of the test utility is to find all the test cases (that is, subclasses of unittest.TestCase) in any file whose name begins with test, automatically build a test suite out of those test cases, and run that suite.
根据 django 文档 当您运行测试时,测试实用程序的默认行为是在名称以 test 开头的任何文件中查找所有测试用例(即unittest.TestCase 的子类),自动构建一个测试套件那些测试用例,并运行该套件。
so try this : python manage.py test tests.py
所以试试这个: python manage.py test tests.py
回答by Doug Murphy
I had the same error as Chris. I had deleted an old model, then run tests.py, but another file (views.py) was still trying to import the deleted model.
我和克里斯有同样的错误。我删除了一个旧模型,然后运行 tests.py,但另一个文件 (views.py) 仍在尝试导入已删除的模型。
When I took out the now-obsolete import statement, problem solved.
当我取出现在已经过时的 import 语句时,问题就解决了。
回答by Steve Bradshaw
Use:
用:
./manage.py shell
./manage.py shell
followed by
其次是
import myapp.tests
import myapp.tests
to find the nature of the import error.
找出导入错误的性质。
回答by lukeaus
Steve Bradshaw's example above works for import errors (thanks Steve).
上面 Steve Bradshaw 的示例适用于导入错误(感谢 Steve)。
Other type of errors (e.g. ValueError) may also cause
其他类型的错误(例如 ValueError)也可能导致
AttributeError: 'module' object has no attribute 'tests'
to see what these errors are
看看这些错误是什么
./manage.py shell
from myapp.tests import SomeTestCase
t = SomeTestCase()
回答by tmin
For my case, I need to create an empty __init__.pyin my app/testsfolder
对于我的情况,我需要在我的文件夹中创建一个空的 __init__.pyapp/tests
回答by zhihong
Got the same error, but checked all the reasons list here, did not fix my problem.
遇到同样的错误,但检查了此处列出的所有原因,并没有解决我的问题。
Finally figure it out that, the reason is that the name of one method that imported but not used yet is not correct. Though it is a stupid error, it happens.
终于搞清楚了,原因是有一个方法导入了还没使用的方法名不对。虽然这是一个愚蠢的错误,但它确实发生了。
回答by Lukasz Dynowski
Make sure that all modules that you are using in your script are not broken. By this I mean check spelling in your import statements.
确保您在脚本中使用的所有模块都没有损坏。我的意思是在导入语句中检查拼写。
# invalid import
from app.model.notification import Notification
# valid import
from app.models.notification import Notification
You can test yours modules by executing imports statements in djano's interactive console.
您可以通过在 djano 的交互式控制台中执行导入语句来测试您的模块。
$root@13faefes8: python manage.py shell
Type "help", "copyright", "credits" or "license" for more information (InteractiveConsole)
>>> from app.model.notification import Notification
Traceback (most recent call last):
File "<console>", line 1, in <module>
ImportError: No module named model.notification
回答by rudyt
I resolved the error "AttributeError: module 'utils' has no attribute 'name_of_my_function' " by fixing a circular import reference. My files manage.py and utils.py each had an import statement pointing at each other.
我通过修复循环导入引用解决了错误“AttributeError: module 'utils' has no attribute 'name_of_my_function'”。我的文件 manage.py 和 utils.py 每个都有一个相互指向的导入语句。
回答by Davide Andrea
I had the same error. It turned out to be because I named my module common.py, yet there already was some other common.py module. All I had to do was to rename my module.
我有同样的错误。原来是因为我将我的模块命名为 common.py,但已经有一些其他的 common.py 模块。我所要做的就是重命名我的模块。

