Python / Django测试运行器导入错误

时间:2015-07-24 14:50:22

标签: python django unit-testing python-3.x

因此,在初始提交之前启动项目并且没有正确设置源代码控制的快乐传统中,我已经离开并破坏了我的Django Test Runner。似乎PATH在某个地方搞砸了,所以这可能不是Django特有的问题。

我可以成功运行django dev服务器并且所有项目功能都可以运行。我在项目中可能有70个测试工作,直到某个时候我引入了一些不能使它们成功的测试。

错误:

======================================================================
ERROR: app.group.tests (unittest.loader.ModuleImportFailure)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/Users/Jamie/.pyenv/versions/3.4.1/lib/python3.4/unittest/case.py", line 58, in testPartExecutor
    yield
  File "/Users/Jamie/.pyenv/versions/3.4.1/lib/python3.4/unittest/case.py", line 577, in run
    testMethod()
  File "/Users/Jamie/.pyenv/versions/3.4.1/lib/python3.4/unittest/loader.py", line 32, in testFailure
    raise exception
ImportError: Failed to import test module: app.group.tests
Traceback (most recent call last):
  File "/Users/Jamie/.pyenv/versions/3.4.1/lib/python3.4/unittest/loader.py", line 312, in _find_tests
    module = self._get_module_from_name(name)
  File "/Users/Jamie/.pyenv/versions/3.4.1/lib/python3.4/unittest/loader.py", line 290, in _get_module_from_name
    __import__(name)
ImportError: No module named 'app.group'

检查标准:

(venv) Jamies-MacBook-Pro:app Jamie$ python3
Python 3.4.1 (default, Jun 30 2015, 16:47:23) 
[GCC 4.2.1 Compatible Apple LLVM 6.1.0 (clang-602.0.53)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.path
['', '/Users/Jamie/.pyenv/versions/3.4.1/lib/python34.zip', '/Users/Jamie/.pyenv/versions/3.4.1/lib/python3.4', '/Users/Jamie/.pyenv/versions/3.4.1/lib/python3.4/plat-darwin', '/Users/Jamie/.pyenv/versions/3.4.1/lib/python3.4/lib-dynload', '/Users/Jamie/.pyenv/versions/venv/lib/python3.4/site-packages']

没有什么可以搞乱PATH,我已经进入了我可以部署的许多未经测试的功能(以及即将发布的日期)的阶段。有什么想法吗?

1 个答案:

答案 0 :(得分:10)

因此,在挖掘之后,事实证明Django Test Runner有一个众所周知的警告。如果根项目文件夹中有dunder-init,则会出现此问题。罪魁祸首是下面的第一个文件:

jamiestrauss@jamies-air-2:~/projects/app$ tree
.
├── __init__.py
├── app
│   ├── __init__.py
│   ├── celery.py
│   ├── settings.py
│   ├── urls.py
│   └── wsgi.py
├── client
│   ├── __init__.py
│   ├── admin.py
│   ├── api.py
│   ├── migrations

当然,Shebang!中存在manage.py,加上dunder-init意味着整个应用程序作为模块导入,这会混淆内部参考。

tl; dr - 如果发生这种情况,请删除__init__.py目录中的根/project/